全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1349
推到 Plurk!
推到 Facebook!

請問一個新手在thread應用上遇到的問題

尚未結案
downmusic
一般會員


發表:4
回覆:6
積分:2
註冊:2005-07-28

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-08-03 11:53:14 IP:59.113.xxx.xxx 未訂閱
請問各位前輩們: 小弟是delphi新手,最近在作thread的練習,碰上了一點小麻煩,情況是這樣, 當form建立時也就產生一個thread,而有一個edit給使用者輸入thread的延遲時間 ,如果當使用者輸入了會導致產生except的內容時,比如說輸入了字串,這時我並不 要讓except的產生導致程式停頓,該如何處理,因為自己嘗試了用try,可是一直無法 做到目標中的狀態,是否能有辦法解決呢?
downmusic
一般會員


發表:4
回覆:6
積分:2
註冊:2005-07-28

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-08-03 12:34:48 IP:59.113.xxx.xxx 未訂閱
忘了附上程式碼 . . . . procedure theTest.Execute; var i, theone, thetwo, thethree : Integer; begin i := 0; repeat if theNO = 1 then begin try theone := StrToInt(Form1.Edit2.Text); if (i mod 2) = 0 then Form1.CheckBox1.Checked := true else Form1.CheckBox1.Checked := false; sleep(theone); i := i 1; except .............. 這裡不知道該怎麼處理比較好 end; end . . . 各位前輩能指導小弟一下嗎?
downmusic
一般會員


發表:4
回覆:6
積分:2
註冊:2005-07-28

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-08-03 16:09:02 IP:59.113.xxx.xxx 未訂閱
經過一番修正,小弟有做到目標中的狀態,把修正後的程式放上來, 不知道是否有關錯誤的地方,有請各位大大指教 . . procedure theTest.Execute; var i, j, theone, thetwo, thethree : Integer; begin i := 0; repeat if theNO = 1 then begin try theone := StrToInt(Form1.Edit2.Text); j := theone; except on E: EConvertError do theone := j; end; if (i mod 2) = 0 then Form1.CheckBox1.Checked := true else Form1.CheckBox1.Checked := false; sleep(theone); i := i 1; end . . .
malanlk
尊榮會員


發表:20
回覆:694
積分:577
註冊:2004-04-19

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-08-04 02:15:39 IP:61.219.xxx.xxx 未訂閱
成是這樣寫有點怪....    如果你這樣寫, 你必須知道當你在修改 Edit 內容時, Thread 正在執行哦.... 當你輸入錯誤資料的瞬間, Thread 可能也在判斷...這裡有同步的問題.    建議把 theone 宣告成全域變數, Edit 所在的 Form1 加個 Button1;    
procedure Form1.Button1Click(..)
begin
  theone := StrToIntDef(Edit1.Text, theone);
end;
...
var 
  theone: Integer;
  IsOdd: Boolean;
...
procedure theTest.Execute;
var  
  i, j, thetwo, thethree : Integer;
begin
  i := 0;
  repeat
  if theNO = 1 then
  begin
    IsOdd := ((i mod 2) = 1);
    Synchronize(SetCheckBox);
    sleep(theone);
    i := i   1;
  end
....    procedure theTest.SetCheckBox;
begin
  Form1.CheckBox1.Checked := not IsOdd;
end;    
順便ㄧ提, 如果你在內文輸入程式碼時 將程式碼用 [code][/code]包起來, 程式的縮排就不會不現了
downmusic
一般會員


發表:4
回覆:6
積分:2
註冊:2005-07-28

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-08-05 10:58:33 IP:59.113.xxx.xxx 未訂閱
多謝malanlk,還學到了發文的技巧
系統時間:2024-05-03 5:39:55
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!