看板 C_Sharp 關於我們 聯絡資訊
※ 引述《scottzz (無心工作,等待機會)》之銘言: : ※ 引述《james732 (好人超)》之銘言: : : 我的想法是這樣的: : : 準備一個 mutex, 一開始把它上鎖 : : 在 C() 的一開始對這個 mutex 再一次上鎖 : : 因為已經鎖著,C() 無法再鎖一次,所以會停下來等待解鎖 : : 然後在 timer() 執行一百次後,把 mutex 給解鎖 : : 這個時候 C() 便可以取得 mutex,並且繼續執行了 : 其實跟WaitHandle概念是一樣,可以使用AutoResetEvent 類別. : 宣告一個 AutoResetEvent are = new AutoResetEvent(false); : 在tx.enable = true;之後 : 將 are.waitone(); 使程序不往下Go. : 待 timer內跑完100次後 再將are.set(); 讓主執行緒再往下Go. 請問一下,這個寫法雖然compiler會過, 可是實際點button的時候會整個卡住, 不知道會是甚麼原因呢? -- AutoResetEvent are = new AutoResetEvent(false); private void button2_Click(object sender, EventArgs e) { MessageBox.Show("start"); timess = 0; timer1.Enabled = true; are.WaitOne(); MessageBox.Show("done"); } private void timer1_Tick(object sender, EventArgs e) { timess++; if (timess > 10) { are.Reset(); timer1.Enabled = false; } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.169.227.253
chancewen:GUI Invoke 05/25 18:31