線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:5804
推到 Plurk!
推到 Facebook!

Application->ProcessMessages()

尚未結案
nanaya
一般會員


發表:25
回覆:33
積分:11
註冊:2004-07-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-07-13 08:36:42 IP:210.202.xxx.xxx 未訂閱
請問各位先進: Application->ProcessMessages()是什麼意思,而且使用時機為何??
hornacek
一般會員


發表:29
回覆:76
積分:21
註冊:2004-02-02

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-07-13 08:46:09 IP:220.135.xxx.xxx 未訂閱
節錄BCB說明檔: void __fastcall ProcessMessages(void); Interrupts the execution of an application so that Windows can process the message queue. Description Call ProcessMessages to permit Windows to process these messages that are currently in the message queue. ProcessMessages cycles the Windows message loop until it is empty and then returns control to the application. 在迴圈中加了這行,程式就不會被迴圈hold住。
nanaya
一般會員


發表:25
回覆:33
積分:11
註冊:2004-07-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-07-13 08:55:39 IP:210.202.xxx.xxx 未訂閱
謝謝hornacek的回應, 意思是說在程式中加入Application->ProcessMessages()後,會讓程式跑起來更順暢嗎?? 那想請問Application->ProcessMessages()放在程式那裡最適當??
nlj859
資深會員


發表:139
回覆:375
積分:322
註冊:2004-03-20

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-07-13 12:05:30 IP:61.30.xxx.xxx 未訂閱
Hello nanaya,    請參考底下連結: http://delphi.ktop.com.tw/topic.php?TOPIC_ID=28249 
nanaya
一般會員


發表:25
回覆:33
積分:11
註冊:2004-07-13

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-07-13 16:47:49 IP:210.202.xxx.xxx 未訂閱
謝謝nlj859的回應, 連結的內容我已看過,可是仍然不是很清楚的來解答我的問題, 不知能否針對我的問題來作解釋,謝謝!!
ENIX007
高階會員


發表:28
回覆:274
積分:185
註冊:2003-11-27

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-07-13 18:15:26 IP:203.70.xxx.xxx 未訂閱
nanaya您好 小弟用一個簡單的程式來說明,您可以試試看,比較其中的差別 class="code">void __fastcall TForm1::Button1Click(TObject *Sender) { for (int i=0; i<150000 ; i ) { // Application->ProcessMessages(); Label1->Caption = i; } } 沒有Application->ProcessMessages();時,Label只會顯示最後的149999... 那是因為在迴圈累加的過程中,應用程式來不及去更新Label所致...(也就是 處理OnPaint訊息) 因此它的使用時機便是在程式處理量大之處,如上所述迴圈的累加,就必須放在 每一次進入迴圈時 程式迷人之處,在於邏輯思考,然而卻也是惱人之處~~
------
程式迷人之處,在於邏輯思考,然而卻也是惱人之處~~
hornacek
一般會員


發表:29
回覆:76
積分:21
註冊:2004-02-02

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-07-13 18:16:56 IP:61.63.xxx.xxx 未訂閱
節錄BCB的說明檔: This example uses two buttons that are long enough to accommodate lengthy captions on a form. When the user clicks the button with the caption Ignore Messages, the code begins to generate a long series of random numbers. If the user tries to resize the form while the handler is running, nothing happens until the handler is finished. When the user clicks the button with the caption Process Messages, more random numbers are generated, but Windows can still respond to a series of mouse events, such as resizing the form.    Note:        How quickly these event handlers run depends on the microprocessor of your computer. A message appears on the form informing you when the handler has finished executing.
void __fastcall TForm1::FormCreate(TObject* Sender)    {
  Button1->Caption = "Ignore Messages";
  Button2->Caption = "Handle Message";
}    void __fastcall TForm1::Button1Click(TObject* Sender)
{
  int x, y;
  for (int i = 0; i < 64000; i  )
 {
    Randomize();
    for (int j = 0; j < 64000; j  )
      y = random(j);
    x = random(i);
  }
  Canvas->TextOut(10, 10, "The Button1Click handler is finished");
}    void __fastcall TForm1::Button2Click(TObject *Sender)    {
  int x, y;
  for (int i = 0; i < 64000; i  )
  {
    Randomize();
    for (int j = 0; j < 64000; j  )
    {
      y = random(j);
      Application->ProcessMessages();
    }
    x = random(i);
  }
  Canvas->TextOut(10, 10, "The Button2Click handler is finished");
}
上面有兩個按鈕,一個是沒加Application->ProcessMessages(),一個是有加,你跑過之後應該就了解加Application->ProcessMessages()的作用在哪了...
nlj859
資深會員


發表:139
回覆:375
積分:322
註冊:2004-03-20

發送簡訊給我
#8 引用回覆 回覆 發表時間:2004-07-13 18:59:06 IP:163.28.xxx.xxx 未訂閱
Hello nanaya, 以樓上的程式來講: "有加"Application->ProcessMessages(); : 處理時間"慢" "沒加"Application->ProcessMessages(); : 處理時間"快" 就這樣差別而已. 如果你運行一個非常耗時的迴圈,那?在這個迴圈結束前,你的程式可能不會回應任何事件,你按按鈕沒有反應,程式設置無法繪製表單,看上去就如同死了一樣,這有時不是很方便,例如於終止迴圈的機會都沒有了。這時你就可以在迴圈中加上這?一句,每次程式運行到這句時,程式就會讓系統回應一下消息,從而使你有機會按按鈕,表單有機會繪製。 相當VB上的DoEvent. 發表人 - nlj859 於 2004/07/13 19:07:55
RogerHer
一般會員


發表:11
回覆:39
積分:10
註冊:2002-03-13

發送簡訊給我
#9 引用回覆 回覆 發表時間:2004-07-13 20:43:04 IP:211.76.xxx.xxx 未訂閱
就 ProcessMessages 字面上的意思來說就是處理 VCL 畫面繪製、滑鼠及鍵盤等訊息(Message),雖然 Windows 是多工的 OS,但是在一個程式裏面若加入無窮迴圈則此程式就無暇處理上面所說的各項訊息,此時就要主動呼叫 ProcessMessages() 來解決。
nanaya
一般會員


發表:25
回覆:33
積分:11
註冊:2004-07-13

發送簡訊給我
#10 引用回覆 回覆 發表時間:2004-07-14 10:27:08 IP:210.202.xxx.xxx 未訂閱
大力感謝各位先進的回覆,對此終於有所了解
系統時間:2024-04-24 11:28:27
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!