不要用sleep函数啊,我用了它之后暂停那几秒的时候那程序就好像死机一样,要暂停过后才有反应的.现在我想要一个暂停的时候那个程序也可以运行的函数.
线程1:
while true do begin
...
sleep(5000); //这暂停的五秒程序就好像死了那样不动了,我需要这段时间也可以按其它按钮什么的.
end;
线程1:
while true do begin
...
sleep(5000); //这暂停的五秒程序就好像死了那样不动了,我需要这段时间也可以按其它按钮什么的.
end;
begin
application.ProcessMessages;
sleep(500);
inc(i);
end;
加上这条就行了,否则的话程序不会到你的应用程序消息队列中读消息的,所以你的程序其它地方当然就动不了.如你说的死了一样
procedure Delay(MSecs: DWORD);
var
BeginTime: DWORD;
begin
BeginTime := GetTickCount;
repeat
Application.ProcessMessages;
until GetTickCount - BeginTime >= MSecs;
end;
unit Unit3;interfaceuses
Classes, SysUtils, syncobjs, Forms;type
Tmythread1 = class(TThread)
private
{ Private declarations }
procedure search;
protected
procedure Execute; override;
end;implementation
uses Unit1;
{ Tmythread1 }procedure Tmythread1.search;
begin
{ Place thread code here }
if Form1.flag1 then begin
Application.ProcessMessages;
sleep(Form1.settime1);
end;
end;procedure Tmythread1.Execute;
begin
{ Place thread code here }
while true do begin
if (Terminated) then exit;
synchronize(search);
end;
end;end.
//////////////////线程///////////////
unit Unit3;interfaceuses
Classes, SysUtils, syncobjs, Forms;type
Tmythread1 = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;implementation
uses Unit1;
{ Tmythread1 }procedure Tmythread1.Execute;
begin
{ Place thread code here }
while Not(Form1.flag1) or (Not Terminated) do
begin
Application.ProcessMessages;
end;
end;
//////////////////线程///////////////
unit Unit3;interfaceuses
Classes, SysUtils, syncobjs, Forms;type
Tmythread1 = class(TThread)
private
{ Private declarations }
procedure search;
protected
procedure Execute; override;
end;implementation
uses Unit1;
{ Tmythread1 }procedure Tmythread1.search;
begin
{ Place thread code here }
if Form1.flag1 then begin
Application.ProcessMessages;
sleep(Form1.settime1);
Terminate;//处理完成后,一定要中断线程
end;
end;procedure Tmythread1.Execute;
begin
{ Place thread code here }
// search;// 如果search过程只需运行一次,就加在这里,取消循环中的调用
while Not Terminated do
begin
Application.ProcessMessages;
search;
end;
end;end.
如果在线程的循环中用了sleep的话,可以不用Application.ProcessMessages,搂主之所以出现不能移动窗体的情况,应该是由于 synchronize(search); 同步执行了search过程,而且search过程中对窗体的显示部分作了修改。如果是这样的话,我觉得
用线程就没有什么意义了。