线程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);这一行就不会.
但结果不对.
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);这一行就不会.
但结果不对.
这句有问题,这里的同步,应该是指同步VCL(因为有界面的VCL不是线程安全的)。
你应该把操作VCL更新的语句放到一过程中,然后:synchronize(操作VCL的过程),
当然,你这个过程看来是需要参数的。
procedure TForm1.BitBtn7Click(Sender: TObject);
begin
FileCountChange.Create(False);
end;
开始执行线程----线程完毕时synchronize(form1.BitBtn7.Click);
又开始TForm1.BitBtn7Click
synchronize(form1.BitBtn7.Click);
加sleep(1);
看看,不在乎时间等待多长,但是死机的问题就没有了
以前我遇到过
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.
用了你的方法,还是不行,鼠标指针移动得很慢,不顺畅,把程序关掉鼠标指针移动就很正常。我的程序是要实现这样的功能,要实时检测C:\临时文件 这个文件夹下的文件数,如果有变化就刷新主程序的一个FileLIstBox1,而用定时器Timer会闪烁很厉害。(奇怪的是我在家里Win200里运行就没有问题,但看到内存使用量几十K地增加,比运行RealPlay还厉害,所以我觉得还是有问题)。我该如何做呢
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.
注意:
Timer1.Enabled=true;