在线程中使用sleep,但是现在外部有个操作需要线程马上响应,但是由于使用的是sleep,所以必须等sleep结束后才能响应后进来的操作。查看SleepEx好像可以实现该功能,不知道有没有那位仁兄用过啊?

解决方案 »

  1.   

    The SleepEx function causes the current thread to enter a wait state until one of the following occurs: An I/O completion callback function is called 
    An asynchronous procedure call (APC) is queued to the thread. 
    The time-out interval elapses
      

  2.   

    http://www.howtodothings.com/showarticle.asp?article=678
      

  3.   

    To aiirii: 抱歉没看懂。To 罗云:你说简单,说个思路啊,MSDN中讲的不是很清楚,可能是我因为水平太硕吧。
      

  4.   

    此函数可以让系统悬挂2000毫秒:
        Sleepex(2000,true);
    在使用上跟Sleep(2000)没有多大的区别
      

  5.   

    要得就是他的特殊功能啊!SleepEx(2000, True);
      

  6.   

    已经搞定,代码如下:
    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.