如何在DELPHI中实现以下VC的语句? ( 积分: 100 )

  • 主题发起人 主题发起人 jingtao
  • 开始时间 开始时间
J

jingtao

Unregistered / Unconfirmed
GUEST, unregistred user!
//使消息泵继续消息循环<br>BOOL PeekAndPump()<br>{<br>MSG msg;<br><br> while(::PeekMessage(&amp;msg,NULL,0,0,PM_NOREMOVE))<br> {<br> if(!AfxGetApp()-&gt;PumpMessage())<br> {<br> ::PostQuitMessage(0);<br> return false;<br> }<br> }<br><br> return true;<br>}<br><br><br>注意:我试验过用Application.ProcessMessages.但是在线程里面不断调用Application.ProcessMessages的话CPU会达到100%
 
//使消息泵继续消息循环<br>BOOL PeekAndPump()<br>{<br>MSG msg;<br><br> while(::PeekMessage(&amp;msg,NULL,0,0,PM_NOREMOVE))<br> {<br> if(!AfxGetApp()-&gt;PumpMessage())<br> {<br> ::PostQuitMessage(0);<br> return false;<br> }<br> }<br><br> return true;<br>}<br><br><br>注意:我试验过用Application.ProcessMessages.但是在线程里面不断调用Application.ProcessMessages的话CPU会达到100%
 
function PeekAndPump(): Bool;<br>var<br> &nbsp;Msg: TMsg;<br>begin<br> &nbsp;Result := False;<br><br> &nbsp;while PeekMessage(Msg, 0, 0, 0, PM_NOREMOVE) do // 当有消息时..<br> &nbsp;begin<br> &nbsp; &nbsp;if(PumpMessage() = False) then // 取出消息并且处理<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;PostQuitMessage(0);<br> &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br><br> &nbsp;Result := True;<br>end;
 
function PumpMessage(): Bool;<br>var<br> &nbsp;m_msgCur: TMsg;<br>begin<br> &nbsp;Result := False;<br><br> &nbsp;if (GetMessage(m_msgCur, 0, 0, 0) = False) then Exit;<br><br> &nbsp;TranslateMessage(m_msgCur);<br> &nbsp;TispatchMessage(m_msgCur);<br><br> &nbsp;Result := True;<br>end;
 
麻子兄动作真快
 
哇,高手现身了
 
乱写的,对不对不知道,似乎这个函数和Application.ProcessMessages差不多,应该是不能一直调用的,除非在没消息的时候选择挂起线程,才能降低CPU占用率,这部分的代码没找到..
 
学习ing。楼主和麻子兄都是高手!呵呵。
 
不行的.各位可以建立一个线程.该线程唯一动作就是调用该函数.然后看CPU.
 
麻子的是OK的.但,程序无法退出了.如果单独在一个线程类里面是可以退出的.
 
BOOL CWinThread::PumpMessage()<br>{<br> &nbsp; &nbsp;_AFX_THREAD_STATE *pState = AfxGetThreadState();<br><br> &nbsp; &nbsp;::GetMessage(&amp;(pState-&gt;m_msgCur), NULL, NULL, NULL))<br> <br> &nbsp; &nbsp;if (!AfxPreTranslateMessage(&amp;(pState-&gt;m_msgCur)))<br> &nbsp; &nbsp;{<br> &nbsp; &nbsp; &nbsp; &nbsp;::TranslateMessage(&amp;(pState-&gt;m_msgCur));<br> &nbsp; &nbsp; &nbsp; &nbsp;::DispatchMessage(&amp;(pState-&gt;m_msgCur));<br> &nbsp; &nbsp;}<br> &nbsp; &nbsp;return TRUE;<br>}<br>http://www.zahui.com/html/1/40092.htm
 
可以参考DELPHI VCL 中用来实现消息循环的代码<br>这是APPLICATION的RUN函数,Application 创建并初始化后就会进入这个过程<br>procedure TApplication.Run;<br>begin<br> &nbsp;FRunning := True;<br> &nbsp;try<br> &nbsp; &nbsp; &nbsp;...<br> &nbsp; &nbsp; &nbsp;repeat<br> &nbsp; &nbsp; &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HandleMessage; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//重复循环调用消息处理函数 HandleMessage<br> &nbsp; &nbsp; &nbsp; &nbsp;except<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HandleException(Self);<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;until Terminated;<br> &nbsp; &nbsp;end;<br> &nbsp;finally<br> &nbsp; &nbsp;FRunning := False;<br> &nbsp;end;<br>end;<br>这是HandleMessage方法,通过调用消息处理函数ProcessMessage,处理消息队列中的消息,如果没有消息处理就调用空闲函数Idle<br>procedure TApplication.HandleMessage;<br>var<br> &nbsp;Msg: TMsg;<br>begin<br> &nbsp;if not ProcessMessage(Msg) then Idle(Msg);<br>end;<br><br>这是ProcessMessage方法,通过调用PeekMessage API从WINDOWS消息队列中取出自己的消息交给DELPHI中的消息处理过程处理,再通过 &nbsp;TranslateMessage 和 DispatchMessage将未处理的消息分发给对应的窗口消息过程或者线程消息过程,楼主需要实现的函数和这个类似<br>function TApplication.ProcessMessage(var Msg: TMsg): Boolean;<br>var<br> &nbsp;Handled: Boolean;<br>begin<br> &nbsp;Result := False;<br> &nbsp;if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then<br> &nbsp;begin<br> &nbsp; &nbsp;Result := True;<br> &nbsp; &nbsp;if Msg.Message &lt;&gt; WM_QUIT then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Handled := False;<br> &nbsp; &nbsp; &nbsp;if Assigned(FOnMessage) then FOnMessage(Msg, Handled);<br> &nbsp; &nbsp; &nbsp;if not IsHintMsg(Msg) and not Handled and not IsMDIMsg(Msg) and<br> &nbsp; &nbsp; &nbsp; &nbsp;not IsKeyMsg(Msg) and not IsDlgMsg(Msg) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;TranslateMessage(Msg);<br> &nbsp; &nbsp; &nbsp; &nbsp;DispatchMessage(Msg);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;FTerminate := True;<br> &nbsp;end;<br>end;<br><br>这是ProcessMessage,作用就是不断调用ProcessMessage将消息队列中的消息全部取出并处理完成。它的一个重要用途就是当RUN过程中在REPEAT循环过程里出现耗时操作时,可以通过直接调用ProcessMessages处理进程中的消息,不至于因为消息未得到即使处理而出现界面卡死的情况。看起来有点像多线程的效果,呵呵~~<br>procedure TApplication.ProcessMessages;<br>var<br> &nbsp;Msg: TMsg;<br>begin<br> &nbsp;while ProcessMessage(Msg) do {loop};<br>end;<br><br>至于ProcessMessages为什么会导致CPU 100%,当队列中没有消息了的时候,ProcessMessage根本不会进入消息处理过程,而是直接跳出,调用中间又没有SLEEP,当然会100%了,呵呵~~
 
GetMessage不得.导致线程其它东西不工作了.
 
GetMessage在没有消息的时候会挂起调用线程等待消息,[:(]
 
谢谢yinju.不过那些我都知道.现在是想出分换代码.因为懒得去翻MFC代码去看那个AfxGetApp()-&gt;PumpMessage()如何实现的了.<br>麻子.努力.嘿嘿.
 
我晕,呵呵,[:D]
 
分数加到300...
 
While Not Terminated Do<br> &nbsp;Begin<br> &nbsp; &nbsp;while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do<br> &nbsp; &nbsp; &nbsp;if Msg.message = WM_NULL then<br> &nbsp; &nbsp; &nbsp; &nbsp;CheckSynchronize<br> &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;TranslateMessage(Msg);<br> &nbsp; &nbsp; &nbsp; &nbsp;DispatchMessage(Msg);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;Sleep(10);<br> &nbsp;End;
 
function PeekAndPump: Bool;<br>begin<br>//请在这里写下你的代码,然后打开任务管理器查看该进程的CPU占用率.注意:不能使用Sleep,否则ReceivePacketsThread将会丢失数据<br>end;<br><br>function ReceivePacketsThread(lp: Pointer): DWORD; stdcall;<br>begin<br>while True do<br>begin<br>PeekAndPump;<br>//从网卡得到数据<br>end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> ReceiveThread: THandle;<br> ThreadId:DWORD;<br>begin<br> ReceiveThread := CreateThread(nil, 0, @ReceivePacketsThread, nil, 0, ThreadId);<br>end;
 
解决了.哈哈哈.
 
后退
顶部