我正在開發一個控制硬件設備的Windows Forms應用程序。我有一個關閉設備的按鈕。點擊事件如下所示:爲什麼我的命令不能立即執行,除非我使用MessageBox.Show()?
Private Sub btnTurnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnTurnOff.Click
device.SetOff() 'Turn off the device
system.threading.thread.sleep(2000) 'Pause for 2 seconds
End Sub
奇怪的是,該設備直到2秒後才關閉。暫停,但如果我在SetOff()命令後立即插入MessageBox,則設備會在2秒之前立即關閉。暫停:
Private Sub btnTurnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnTurnOff.Click
device.SetOff() 'turn off the device
MessageBox.Show("Device is now off")
system.threading.thread.sleep(2000) 'Pause for 2 seconds
End Sub
爲什麼這段代碼的行爲是這樣的?
'MessageBox.Show'被阻塞,所以直到你點擊「OK」,它纔會進入'Thread.Sleep'。 – vcsjones 2012-03-21 19:04:04