延时方法!请指教,有没有更好的方法!(100分)

  • 主题发起人 主题发起人 保龙
  • 开始时间 开始时间

保龙

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; i:integer;<br>begin<br>&nbsp; for i:=0 to 1000 do<br>&nbsp; begin<br>&nbsp; sleep(100); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //转让控制权,但按其它按钮时不能向应<br>&nbsp; form1.Text:=inttostr(i);<br>&nbsp; end;<br>end;<br><br>procedure TimeDelay(Dt:Double);<br>var<br>&nbsp; TT:Double;<br>begin<br>&nbsp; TT:=GetTickCount();<br>&nbsp; &nbsp;while GetTickCount()-TT&lt;DT do<br>&nbsp; &nbsp; &nbsp;Application.ProcessMessages;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br>&nbsp;TT:Double;<br>&nbsp;i:integer;<br>begin<br>&nbsp; for i:=0 to 1000 do<br>&nbsp; begin<br>&nbsp; &nbsp; TimeDelay(100); &nbsp; &nbsp; &nbsp; //程序延时后按按钮有向应,但CPU资源使用100%<br>&nbsp; &nbsp; form1.Text:=inttostr(i);<br>&nbsp; end;<br>end;<br>
 
如果对时间精度要求不高,Timer是个不错的选择<br>
 
&gt;&gt;&gt;CPU资源使用100%<br>这是肯定的,无论多么好的Cpu,遇到死循环也肯定是100%<br><br>如果需要又能响应按键,又能使Button1Click在循环中延时,是否可以使用多线程。。<br>当单击Button时候创建一个线程,线程体就是<br>&nbsp; var<br>&nbsp; &nbsp; i:integer;<br>&nbsp; begin<br>&nbsp; &nbsp; for i:=0 to 1000 do<br>&nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; sleep(100); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//转让控制权,但按其它按钮时不能向应<br>&nbsp; &nbsp; &nbsp; form1.Text:=inttostr(i);<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>这样就可以了。<br>如果,需要线程可以中途退出,那可以把for循环修为 while i&lt;=1000 and Run do ...<br>然后其他终止线程按钮的实践可以置Run:=false;这样在下次循环时候就会退出线程。。
 
procedure TimeDelay(Dt:DWORD);<br>var<br>&nbsp; TT:DWORD;<br>begin<br>&nbsp; TT:=GetTickCount();<br>&nbsp; &nbsp;while GetTickCount()-TT&lt;DT do<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;Application.ProcessMessages;<br>&nbsp; &nbsp; Sleep(1);//这才是关键<br>&nbsp; end;<br>end;<br>
 
procedure TForm1.Button2Click(Sender: TObject);<br>var<br>&nbsp;TT:Double;<br>&nbsp;i:integer;<br>begin<br>&nbsp; for i:=0 to 1000 do<br>&nbsp; begin<br>&nbsp; &nbsp; TimeDelay(100); &nbsp; &nbsp; &nbsp; //程序延时后按按钮有向应,但CPU资源使用100%<br>&nbsp; &nbsp; form1.Text:=inttostr(i);<br>&nbsp; &nbsp;Application.postmessage;<br>&nbsp; end;<br>end;<br>
 
请创建一个子线程 线程体为:<br>&nbsp; var<br>&nbsp; &nbsp; i:integer;<br>&nbsp; begin<br>&nbsp; &nbsp; for i:=0 to 1000 do<br>&nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; sleep(100); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//转让控制权,但按其它按钮时不能向应<br>&nbsp; &nbsp; &nbsp; form1.Text:=inttostr(i);<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>就可以了。。
 
既Sleep,同时也ProcessMessages 就可以了,<br>否则,没有别的办法
 
sleep 只是当前线程。。。主线程并不受影响。。
 
The SetTimer function creates a timer with the specified time-out value. <br><br>UINT SetTimer(<br><br>&nbsp; &nbsp; HWND hWnd, // handle of window for timer messages<br>&nbsp; &nbsp; UINT nIDEvent, // timer identifier<br>&nbsp; &nbsp; UINT uElapse, // time-out value<br>&nbsp; &nbsp; TIMERPROC lpTimerFunc // address of timer procedure<br>&nbsp; &nbsp;); <br>&nbsp;<br><br>Parameters<br><br>hWnd<br><br>Identifies the window to be associated with the timer. This window must be owned by the calling thread. If this parameter is NULL, no window is associated with the timer and the nIDEvent parameter is ignored. <br><br>nIDEvent<br><br>Specifies a nonzero timer identifier. If the hWnd parameter is NULL, this parameter is ignored. <br><br>uElapse<br><br>Specifies the time-out value, in milliseconds. <br><br>lpTimerFunc<br><br>Points to the function to be notified when the time-out value elapses. For more information about the function, see TimerProc. <br>If lpTimerFunc is NULL, the system posts a WM_TIMER message to the application queue. The hwnd member of the message's MSG structure contains the value of the hWnd parameter. <br><br>&nbsp;<br><br>Return Values<br><br>If the function succeeds, the return value is an integer identifying the new timer. An application can pass this value, or the string identifier, if it exists, to the KillTimer function to destroy the timer. If the function fails to create a timer, the return value is zero. <br><br>Remarks<br><br>An application can process WM_TIMER messages by including a WM_TIMER case statement in the window procedure or by specifying a TimerProc callback function when creating the timer. When you specify a TimerProc callback function, the DispatchMessage function simply calls the callback function instead of the window procedure. Therefore, you need to dispatch messages in the calling thread, even when you use TimerProc instead of processing WM_TIMER. <br><br>The wParam parameter of the WM_TIMER message contains the value of the nIDEvent parameter. <br>
 
用多媒体定时器,毫秒级,很精确。
 
用线程吧.
 
什么是?"多媒体定时器"怎样用的?<br>
 
Sleep()和Application.postmessage一定使用?
 
while GetTickCount()-TT&lt;DT do<br>begin<br>&nbsp; &nbsp; &nbsp;Application.ProcessMessages;<br>&nbsp; &nbsp; &nbsp;Sleep(1);<br>end;<br><br>
 
接受答案,并分配积分<br>
 

Similar threads

I
回复
0
查看
690
import
I
I
回复
0
查看
597
import
I
I
回复
0
查看
701
import
I
I
回复
0
查看
501
import
I
后退
顶部