怎麼可能?爲什麼?定時器只是在調用MessageBox.Show()
我有一個,如果沒有網絡連接被稱爲計時器,如方法下:
public void Foo() {
for (int i = 0, count = MailList.CheckedItems.Count; i < count; i++) {
/* Check for network available connection in computer
public bool HasConnection() {
return System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
}
*/
if (!net.HasConnection()) {
SearchNetworkConnection.Start(); //start the timer
}
}
}
和Timer
的_Tick
方法:
private void SearchNetworkConnection_Tick(object sender, EventArgs e) {
++ATTEMPRECONNECT;
string currentState = "attemp reconnect..";
MessageBox.Show(currentState, "..", MessageBoxButtons.OK, MessageBoxIcon.Warning);
if (ATTEMPRECONNECT >= ATTEMPRECONNECTLIMIT) {
//do abort all process
SearchNetworkConnection.Stop();
}
}
只是如果這樣的作品很奇怪,我在SearchNetworkConnection.Start()
之後致電MessageBox.Show()
。
換句話說,這是行不通的,定時器將不會運行:
if (!net.HasConnection()) {
SearchNetworkConnection.Start();
}
調用MessageBox.Show()
,它工作正常:
if (!net.HasConnection()) {
SearchNetworkConnection.Start();
MessageBox.Show("lol");
}
,如果它是有用的,Foo()
方法運行在線程上。
我希望這很清楚。任何幫助非常感謝,提前感謝! :)
UPDATE
這樣。我認爲這是一個有點怪。我爲一些測試寫了一個簡單的代碼。我很驚訝,錯誤仍在繼續。 下來的代碼工作正常。但如果你更改順序
timer.Start();
DialogResult result = MessageBox.Show(text, caption);
到
DialogResult result = MessageBox.Show(text, caption);
timer.Start();
它不工作。定時器不啓動。
public static DialogResult Show(string text, string caption,int dellay)
{
Timer timer = new Timer();
timer.Interval = dellay;
timer.Start();
DialogResult result = MessageBox.Show(text, caption);
timer.Tick += new EventHandler(delegate
{
IntPtr handle = FindWindow(null, caption);
if (handle != IntPtr.Zero)
{
IntPtr hresult = SendMessage(handle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
if (hresult == IntPtr.Zero)
{
timer.Stop();
timer.Dispose();
}
}
});
return result;
}
即使計時器已經啓動,您的'Foo'方法也會調用Timer.Start。是否有可能調用'開始'重置時間間隔?也許你應該寫'if(!net.HasConnection()&&!SearchNetworkConnection.Enabled)'。 – 2011-12-31 13:53:17