How to stop or kill an asynchronous delegate?
I call an asynchronous method from a main thread. How to kill or stop it. Thanks. This is my codes:
SendBroadcastTickerAsyncCaller caller = new Sen开发者_Go百科dBroadcastTickerAsyncCaller(SendBroadcastTickerAsync);
IAsyncResult result = caller.BeginInvoke(Floor, data,
new AsyncCallback(SendBroadcastTickerAsyncCallbackMethod), null);
SendBroadcastTickerAsyncCaller is an asynchronous delegate and SendBroadcastTickerAsync is an asynchronous method.
Please help me how to kill (or stop) it. Thanks.
It needs to kill itself by ending its execution. You can tell it to do this by setting a flag which it periodically checks and if it has been set then it comes out of whatever loop it is in. You will need to mark the boolean flag as volatile so that both threads see the live value.
Killing a thread in other ways is possible but not usually (ever?) appropriate.
Unless the server has a kill method exposed, you probably can only set a BOOL that you check in your callback and if true don't do anything.
精彩评论