不要用sleep函数啊,我用了它之后暂停那几秒的时候那程序就好像死机一样,要暂停过后才有反应的.现在我想要一个暂停的时候那个程序也可以运行的函数.
线程1: 
 while true do begin
 ...
 sleep(5000); //这暂停的五秒程序就好像死了那样不动了,我需要这段时间也可以按其它按钮什么的.
 end;

解决方案 »

  1.   

    那还叫什么暂停?while i<=10  do
    begin
      application.ProcessMessages;
      sleep(500);
      inc(i);
    end;
      

  2.   

    楼主想怎样暂停?to jinjazz 分成10段暂停?真行....
      

  3.   

    你试一下将sleep里面的数值变为5000,你看你还移动窗口位置的时候觉不觉得它慢??我的意思是暂停的时候也可以拉动窗口的位置,不至于要等到它暂停5秒后再运行,如果象你说的那样,我不如将sleep(500)改为sleep(1)?
      

  4.   

    application.ProcessMessages;
    加上这条就行了,否则的话程序不会到你的应用程序消息队列中读消息的,所以你的程序其它地方当然就动不了.如你说的死了一样
      

  5.   

    延时:
    procedure Delay(MSecs: DWORD);
    var
      BeginTime: DWORD;
    begin
      BeginTime := GetTickCount;
      repeat
        Application.ProcessMessages;
      until GetTickCount - BeginTime >= MSecs;
    end;
      

  6.   

    to  GoldShield(金盾):不行哦,下面是我调用线程的源代码,暂停的时候还是拉不动窗体,像死机那样.//////////////////线程///////////////
    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.
      

  7.   

    to  fei19790920(饭桶的马甲(抵制日货)) :THX,你给我的函数可以用,但是MSecs是怎样计算法?是1000等于1秒吗???秒数准不准确的?
      

  8.   

    应该是这句的原因  synchronize(search);
      

  9.   

    这么简单的问题,何必那么麻烦:
    //////////////////线程///////////////
    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;
      

  10.   

    to  fei19790920(饭桶的马甲(抵制日货)) :不行啊,那个函数成天repeat好费资源啊,我看那程序利用99%资源,不行啊,用不了啊,有没有更好的但不费资源的?sleep函数是好,不费资源,但又像死机那样不好用,唉....如果sleep函数和你给我的那个函数可以结合在一起就好了.
      

  11.   

    很奇怪,既然是线程中的循环,为什么还要用Application.ProcessMessages;
      

  12.   

    没有,我那个procedure Tmythread1.search;过程里面还有其它代码的,要循环去调用的.
      

  13.   

    线程中的循环也要占用CPU时间,如果是死循环,也会死机加上“Application.ProcessMessages;”让程序给CPU处理其它事情的时间
      

  14.   

    再帮你改一下:
    //////////////////线程///////////////
    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.
      

  15.   

    //线程中的循环也要占用CPU时间,如果是死循环,也会死机//加上“Application.ProcessMessages;”让程序给CPU处理其它事情的时间如果循环一直进行着的话,当然cpu的占用率会很高,但是主线程还是可以处理处理消息的,
    如果在线程的循环中用了sleep的话,可以不用Application.ProcessMessages,搂主之所以出现不能移动窗体的情况,应该是由于 synchronize(search); 同步执行了search过程,而且search过程中对窗体的显示部分作了修改。如果是这样的话,我觉得
    用线程就没有什么意义了。