线程FileCountChange单元如下:
unit Unit6;
interfaceuses
  Windows, Messages, SysUtils,Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Grids, DBGrids, ExtCtrls, Calendar, Spin, ScktComp,
  jpeg, Buttons, ComCtrls, Menus,registry,inifiles, FileCtrl;type
  FileCountChange = class(TThread)
  private
    { Private declarations }
  protected
    procedure Execute; override;
  end;implementation
 uses  Unit1;
{ Important: Methods and properties of objects in VCL can only be used in a
  method called using Synchronize, for example,      Synchronize(UpdateCaption);  and UpdateCaption could look like,    procedure FileCountChange.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }{ FileCountChange }procedure FileCountChange.Execute;
var
  Search:TSearchRec;
  iCount:integer;//现在文件数
  jCount:integer;//原来文件数
  FilePath:string;
begin
  FilePath:='C:\临时文件\';
  Try
    iCount:=FindFirst(FilePath +'*.jpg',faAnyFile,Search);
    if  iCount=0  then
    repeat
      iCount:=iCount+1;
    until    FindNext(Search)<>0;
    jCount:=strtoint(form1.edit6.text);
    form1.edit6.text:=inttostr(iCount);
    if  jCount<>iCount  then  form1.FileListBox1.Update;
  finally
    FindClose(Search);
  end;
  synchronize(form1.BitBtn7.Click);//与form1.BitBtn7.Click同步
end;end.
然后在主程序的Form1窗口:
procedure TForm1.BitBtn7Click(Sender: TObject);
begin
FileCountChange.Create(False);
end;当按了以上这个按钮后鼠标移动很慢,然后就死机.
去掉synchronize(form1.BitBtn7.Click);这一行就不会.
但结果不对.

解决方案 »

  1.   

    synchronize(form1.BitBtn7.Click);
    这句有问题,这里的同步,应该是指同步VCL(因为有界面的VCL不是线程安全的)。
    你应该把操作VCL更新的语句放到一过程中,然后:synchronize(操作VCL的过程),
    当然,你这个过程看来是需要参数的。
      

  2.   

    进入死循环了吧:   
     procedure TForm1.BitBtn7Click(Sender: TObject);
    begin
    FileCountChange.Create(False);
    end;
    开始执行线程----线程完毕时synchronize(form1.BitBtn7.Click);
    又开始TForm1.BitBtn7Click
      

  3.   

    更改方法以:  在你的附属线程里不要涉及到类似edit6.text的语句,设计VCL的东西全部由主线程来处理,如果需要用到主线程中edit6.text的数据,可以用变量传递
      

  4.   

    你在这一句后面
    synchronize(form1.BitBtn7.Click);
    加sleep(1);
    看看,不在乎时间等待多长,但是死机的问题就没有了
    以前我遇到过
      

  5.   

    我专门给你试了,就不明白你干什么用,是一个死循环,更改后的就没有死机了
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Buttons,Unit2, FileCtrl;type
      TForm1 = class(TForm)
        BitBtn1: TBitBtn;
        Edit1: TEdit;
        FileListBox1: TFileListBox;
        BitBtn2: TBitBtn;
        procedure BitBtn1Click(Sender: TObject);
        procedure BitBtn2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      MyFileCountChange: MyThread;    // variable (type see above)implementation{$R *.dfm}procedure TForm1.BitBtn1Click(Sender: TObject);
    begin
      MyFileCountChange := MyThread.Create('C:\临时文件\',10000);
      MyFileCountChange.FreeOnTerminate:=True;
      MyFileCountChange.Resume;
      //MyFileCountChange.OnTerminate:=Thread1Done;
    end;procedure TForm1.BitBtn2Click(Sender: TObject);
    begin
        MyFileCountChange.Terminate;
    end;end.//---------------------------------------------------------------------------
    unit Unit2;interfaceuses
      Classes,SysUtils;type
      MyThread = class(TThread)
      private
        FilePath:string;
        iCount:integer;//现在文件数
        jCount:integer;//原来文件数
        Fcount:integer;
        { Private declarations }
      protected
        procedure Execute; override;
      public
        { Public declarations }
        constructor create(MyPath:string;Mynumber:integer);     {线程构造函数}
      end;implementationuses Unit1;{ Important: Methods and properties of objects in VCL or CLX can only be used
      in a method called using Synchronize, for example,      Synchronize(UpdateCaption);  and UpdateCaption could look like,    procedure MyThread.UpdateCaption;
        begin
          Form1.Caption := 'Updated in a thread';
        end; }{ MyThread }constructor MyThread.create(MyPath:String;Mynumber:integer);     {线程构造函数}
    begin
      inherited Create(true);
      FilePath:=MyPath;
      Fcount:=Mynumber;
      FreeOnTerminate:=true;
    end;procedure MyThread.Execute;
    var
      Search:TSearchRec;
    begin
      { Place thread code here }
      //FilePath:='C:\临时文件\';
      Try
        iCount:=FindFirst(FilePath +'*.jpg',faAnyFile,Search);
        if  iCount=0  then
        repeat
          iCount:=iCount+1;
        until    FindNext(Search)<>0;
        jCount:=Fcount;
        form1.edit1.text:=inttostr(iCount);
        if  jCount<>iCount  then  form1.FileListBox1.Update;
      finally
        FindClose(Search);
      end;
      if not terminated
        then
         synchronize(form1.BitBtn1.Click);//与form1.BitBtn7.Click同步
    end;end.
      

  6.   

    cjstar(ミ★木木ミ):
    用了你的方法,还是不行,鼠标指针移动得很慢,不顺畅,把程序关掉鼠标指针移动就很正常。我的程序是要实现这样的功能,要实时检测C:\临时文件 这个文件夹下的文件数,如果有变化就刷新主程序的一个FileLIstBox1,而用定时器Timer会闪烁很厉害。(奇怪的是我在家里Win200里运行就没有问题,但看到内存使用量几十K地增加,比运行RealPlay还厉害,所以我觉得还是有问题)。我该如何做呢
      

  7.   

    就是,你早说你要做什么用,下面的完全可以满足你的需求了吧(加Timer1配合使用,你可以设置Timer1的时间,多长时间执行一次):
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Buttons,Unit2, FileCtrl, ExtCtrls;type
      TForm1 = class(TForm)
        Edit1: TEdit;
        FileListBox1: TFileListBox;
        Timer1: TTimer;
        procedure Timer1Timer(Sender: TObject);
      private
        { Private declarations }
        procedure Thread1Done(sende:Tobject);
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      MyFileCountChange: MyThread;    // variable (type see above)implementation{$R *.dfm}procedure TForm1.Thread1Done(sende:Tobject);
    begin
        Timer1.Enabled:=true;
    end;procedure TForm1.Timer1Timer(Sender: TObject);
    begin
      Timer1.Enabled:=false;
      MyFileCountChange := MyThread.Create('C:\临时文件\',strtoint(edit1.Text));
      MyFileCountChange.OnTerminate:=Thread1Done;
    end;
    end.//=============================================================================
    unit Unit2;interfaceuses
      Classes,SysUtils;type
      MyThread = class(TThread)
      private
        FilePath:string;
        iCount:integer;//现在文件数
        jCount:integer;//原来文件数
        Fcount:integer;
        { Private declarations }
      protected
        procedure Execute; override;
      public
        { Public declarations }
        constructor create(MyPath:string;Mynumber:integer);     {线程构造函数}
      end;implementationuses Unit1;{ Important: Methods and properties of objects in VCL or CLX can only be used
      in a method called using Synchronize, for example,      Synchronize(UpdateCaption);  and UpdateCaption could look like,    procedure MyThread.UpdateCaption;
        begin
          Form1.Caption := 'Updated in a thread';
        end; }{ MyThread }constructor MyThread.create(MyPath:String;Mynumber:integer);     {线程构造函数}
    begin
      inherited Create(false);
      FilePath:=MyPath;
      Fcount:=Mynumber;
      FreeOnTerminate:=true;
    end;procedure MyThread.Execute;
    var
      Search:TSearchRec;
    begin
      { Place thread code here }
      //FilePath:='C:\临时文件\';
      Try
        iCount:=FindFirst(FilePath +'*.jpg',faAnyFile,Search);
        if  iCount=0  then
        repeat
          iCount:=iCount+1;
        until    FindNext(Search)<>0;
        jCount:=Fcount;
        form1.edit1.text:=inttostr(iCount);
        if  jCount<>iCount  then  form1.FileListBox1.Update;
      finally
        FindClose(Search);
      end;
    end;end.
      

  8.   

    我测试了,可以的呀,
    注意:
    Timer1.Enabled=true;