2
我有定時代碼檢查死對等。我使用System.Threading.Timer來做到這一點。檢測代碼的工作,我這樣稱呼它:c#線程計時器需要停止自己的異步套接字代碼
Timer SocketKPATimer = new Timer(delegate { timedcode(state); }, null, 5000, 5000);
其中狀態是所使用到
Socket handler = state.workSocket;
,後來成爲StateObject ...
handler.Shutdown(SocketShutdown.Both);
//I need to stop the Timer here
的方法本身被定義作爲 static void timedcode(StateObject state) {...}
現在的問題是,在我發現對端出現問題,我需要SocketKPATimer停止,而我不能。 如何修改時間碼,以便我可以停止計時器?
是的,這確實是我的testcode。在創建SocketKPATimer之後,我確實需要移動'state.TimerThingy = SocketKPATimer;'否則代碼在拋棄定時器時拋出nullreference異常。非常感謝你。 – ZoOl007