在线程中使用sleep,但是现在外部有个操作需要线程马上响应,但是由于使用的是sleep,所以必须等sleep结束后才能响应后进来的操作。查看SleepEx好像可以实现该功能,不知道有没有那位仁兄用过啊?
解决方案 »
- 关于cxGrid 合并单元格求和问题
- idhttp post 参数 同时夹带 cookie 的问题
- IdFTP控件,下载下来的文件名变短了,请各位朋友给予帮助
- 算法?????????????????????
- 如何把一幅bmp图像存进sql server2000表中的image字段
- 调用API不成功
- 关于不同FORM之间的数据交流问题!!!!!
- 迟到啦,第二课,打印,嘻嘻嘻,高的,别来啦,俺怕涮,嘻嘻嘻
- 如何关闭特定的子窗口?
- 俄罗斯方块源程序
- SQL Server数据库里某表的一字段存放的是varbinary数据,如何取到一byte数组里面?
- 原生ADO打开Excel并赋值到一个动态二维数组的问题
An asynchronous procedure call (APC) is queued to the thread.
The time-out interval elapses
Sleepex(2000,true);
在使用上跟Sleep(2000)没有多大的区别
unit main;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DownLoadFileDataThrd;type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
FDownLoadThrd: TDownLoadFileDataThread;
procedure DoOnDownEven(sender: TObject);
procedure WakeupDownThrd;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}{ TForm1 }
procedure WakeupDownThrdproc(const evenFlag: Integer); stdcall;
begin
TForm1(evenFlag).WakeupDownThrd;
end;procedure TForm1.DoOnDownEven(sender: TObject);
begin
self.Memo1.Lines.Add('start sleepEx(10000), Time:'+ TimetoStr(now));
sleepEx(10000, True);
self.Memo1.Lines.Add('End sleepEx(10000), Time:'+ TimetoStr(now));end;procedure TForm1.Button1Click(Sender: TObject);
begin
self.FDownLoadThrd := TDownLoadFileDataThread.Create;
self.FDownLoadThrd.OnExecute := self.DoOnDownEven;
self.FDownLoadThrd.Resume;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
self.FDownLoadThrd.Terminate;
QueueUserAPC(@WakeupDownThrdproc, self.FDownLoadThrd.Handle, DWORD(self));
self.FDownLoadThrd.Free;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
QueueUserAPC(@WakeupDownThrdproc, self.FDownLoadThrd.Handle, DWORD(self));
end;procedure TForm1.WakeupDownThrd;
begin
Memo1.Lines.Add('aaa');
end;end.unit DownLoadFileDataThrd;interfaceuses
Classes, SysUtils, Windows;type
TDownLoadFileDataThread = class(TThread)
private
{ Private declarations } FOnExecute: TNotifyEvent;
procedure DoExecute;
protected
procedure Execute; override; public
Constructor Create;
destructor Destroy; override;
property OnExecute: TNotifyEvent read FOnExecute write FOnExecute;
end;implementation{ 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 TDownLoadFileDataThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ TDownLoadFileDataThread }constructor TDownLoadFileDataThread.Create;
begin
Inherited Create(True);
end;destructor TDownLoadFileDataThread.Destroy;
begin
inherited;
end;procedure TDownLoadFileDataThread.DoExecute;
begin
if Assigned(FOnExecute) then
FOnExecute(self);
end;procedure TDownLoadFileDataThread.Execute;
begin
{ Place thread code here }
while not Terminated do
begin
DoExecute;
end;
end;end.