>>程序出现未响应(自己写的程序,非病毒)?
有什么过程,操作时间过长,占用cpu过高

解决方案 »

  1.   

    重点跟踪initialization段和Form的OnCreate事件。
      

  2.   

    是这样的。我写这个程序是想完成文件恢复功能。写到扫描目录项这一块的时候,再测试程序,结果就出现未响应的情况!内存占用6400k。以下是部分代码:
    unit work;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Buttons, ComCtrls,StrUtils;type
       TrecoverItem=Record
           strFileName:String;   //找到的文件名
           strFirstCluster:string; //文件首簇
           strFileSize:string;  //文件大小
       end;
      TForm8 = class(TForm)
        Label1: TLabel;
        ListView1: TListView;
        BitBtn1: TBitBtn;
        BitBtn2: TBitBtn;
        procedure FormCreate(Sender: TObject);
        procedure BitBtn1Click(Sender: TObject);
        procedure BitBtn2Click(Sender: TObject);
        procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
          Selected: Boolean);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form8: TForm8;
      Path:string;
      strItem:TrecoverItem;
    implementation
    uses about,Unit1,func;{$R *.dfm}procedure TForm8.FormCreate(Sender: TObject);
    var
      I,x:integer;
      ListItem:TListItem;
      lgDrv, capability:TListColumn; //lgDrv 逻辑磁盘(分区) capability  容量,性能
      k,disksize:int64;
      drive:pchar;
      userFreeBytes,totalBytes,freeBytes:Int64;
    begin
      strItem.strFileName:='';
      ListView1.Parent:=Self;
      ListView1.Align:=alClient;
      ListView1.ViewStyle:=vsReport;
      lgDrv:=ListView1.Columns.Add;
      lgDrv.Caption:='分区';
      capability:=ListView1.Columns.Add;
      capability.Caption:='容量';  disksize:=0;
      for i:=99 to 122 do      // 'c'-'z';
        begin
          drive:=pchar(chr(i)+':\');
          x:=GetDriveType(drive);
          if x=3 then              //3:代表介质是硬盘.
             begin
               GetDiskFreeSpaceEx(drive,userFreeBytes,totalBytes,@freeBytes);
               k:=1000000000;
               disksize:=totalBytes div k;
               ListItem:=ListView1.Items.Add;
               ListItem.Caption:=chr(i)+':\';
               ListItem.SubItems.Add(Formatfloat('###,##0',disksize)+'G');
             end;
        end;
    Path:='';end;procedure TForm8.BitBtn1Click(Sender: TObject);
    begin
      about.Form2.Show;
    end;procedure TForm8.BitBtn2Click(Sender: TObject);
    var
      info:Pchar;
      lgDrv:Pchar;
      hDeviceHandle:Thandle;
      first_position:integer;
      num_of_item,i:integer;
      temp1,temp2,low,high:string;
      ListItem:TListItem;
    begin
      num_of_item:=0;
      i:=3;
      temp1:='';
      if Path<>'' then
         begin
           showmessage('Ok');    //已选择磁盘
           // 扫描根目录区,并把被删除的文件名显示在列表栏中。
           if  not func.IsFAT32(Path) then
               begin
                 showmessage('该分区非fat32文件系统!');
                 ListView1.SetFocus;
               end;
           func.ReadBpB(Path);
           first_position:=(func.BpB.reserved_sector+func.BpB.num_of_fat*func.BpB.sector_per_fat)*func.BpB.bytes_per_sector; //根目录区起始位置!
           lgDrv:=PChar('\\.\'+LeftStr(Path,1)+':');
           hDeviceHandle:=CreateFile(lgDrv,GENERIC_READ,FILE_SHARE_READ or FILE_SHARE_WRITE,nil,OPEN_EXISTING,0,0);
           if (hDeviceHandle<>INVALID_HANDLE_VALUE) then  //打开逻辑磁盘成功
              begin
                info:=allocmem(32);
                FileSeek(hDeviceHandle,first_position,0);
               // if  FileRead(hDeviceHandle,info[0],32)<>32 then
               //     raise  exception.Create('读数据错误');
                while (info[0]<>'00') do
                  begin
                    if  func.StrToIntASHex(info[0])=$E5 then
                        if  not ((func.StrToIntASHex(info[11]) and 16)=16) then   //非子目录
                            if  ((func.StrToIntASHex(info[11]) and $0f)=$0f) then   //长文件名目录项
                                begin
                                  num_of_item:=(func.StrToIntASHex(info[0]) and $3f);   //取得目录项个数                              repeat
                                    begin
                                     //目录项之一
                                       strItem.strFileName:=string(info[1]);
                                       repeat
                                         begin
                                           strItem.strFileName:=strItem.strFileName+string(info[i]);
                                           i:=i+2;
                                           if i=9 then
                                              i:=i+5;     //offset  0EH
                                           if i=26 then
                                              i:=i+2;     //offset  1CH
                                         end;
                                       until ( (i=32) or (func.StrToIntASHex(info[i])=255) );                                   first_position:=first_position+32;
                                       FileSeek(hDeviceHandle,first_position,0);                                   freemem(info);
                                       info:=allocmem(32);                                   if  FileRead(hDeviceHandle,info[0],32)<>32 then
                                           raise  exception.Create('读数据错误');
                                       i:=3;
                                       temp1:=temp1+strItem.strFileName;
                                       strItem.strFileName:='';
                                       num_of_item:=num_of_item-1;
                                    end;
                                  until  num_of_item=0;
                                  ListItem.Caption:=temp1;
                                  first_position:=first_position+32;
                                  FileSeek(hDeviceHandle,first_position,0);                              freemem(info);
                                  info:=allocmem(32);                              if  FileRead(hDeviceHandle,info[0],32)<>32 then
                                      raise  exception.Create('读数据错误');
                                  low:=func.IntToStrAsHex(integer(info[20]));
                                  high:=func.IntToStrAsHex(integer(info[21]));
                                  temp1:=high+low;  // 文件起始簇号的高16位
                                  low:=func.IntToStrAsHex(integer(info[26]));
                                  high:=func.IntToStrAsHex(integer(info[27]));
                                  strItem.strFirstCluster:=inttostr(func.StrToIntASHex(temp1+high+low));
                                  low:=func.IntToStrAsHex(integer(info[28]));
                                  high:=func.IntToStrAsHex(integer(info[29]));
                                  temp2:=high+low;
                                  low:=func.IntToStrAsHex(integer(info[30]));
                                  high:=func.IntToStrAsHex(integer(info[31]));
                                  //文件大小
                                  strItem.strFileSize:=inttostr(func.StrToIntASHex(temp2+high+low));
                                  ListItem.SubItems.Add(strItem.strFirstCluster);
                                  ListItem.SubItems.Add(strItem.strFileSize);
                                  unit1.Form1.ListView1.Items.AddItem(ListItem);
                                end
                            else
                                begin
                                //非长文件名目录项
                                //Last Edited at 2006-4-22 wenchunl.
                                end;
                     //读下一个目录项
                    first_position:=first_position+32;                freemem(info);
                    info:=allocmem(32);                FileRead(hDeviceHandle,info[0],32);
                  end;
              end;
         end
      else
         begin
           showmessage('Error'); //尚未选择,要求重新选择
           ListView1.SetFocus;
         end;
      unit1.Form1.Show;
    end;procedure TForm8.ListView1SelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
    begin
      Path:=Item.Caption;
    end;end.谢谢大家支持!