I'd like to stop immediately a method if I press cancel in the "processing" animation screen. I'm using Async call through delegates. Is it possible to stop immediately the execution of the method through delegates?
Thanks :)
If you think of delegates as being similar to interface definitions for a specific type of method, you can start to see why delegates exist. They allow clients of our delegates to ignore all the details of their implementations - even their names!
When the return type is not void as above in my case it is int. Methods with Int return types are added to the delegate instance and will be executed as per the addition sequence but the variable that is holding the return type value will have the value return from the method that is executed at the end.
Delegates allow methods to be passed as parameters. Delegates can be used to define callback methods. Delegates can be chained together; for example, multiple methods can be called on a single event. Methods don't have to match the delegate type exactly.
public delegate void Del(string message); A delegate object is normally constructed by providing the name of the method the delegate will wrap, or with a lambda expression. Once a delegate is instantiated, a method call made to the delegate will be passed by the delegate to that method.
No - at least, not cleanly. (I don't include Thread.Abort
as "clean".) What you should do is have a mechanism to tell the delegate that you're trying to cancel it. Your delegate would then need to occasionally check that cancellation flag and react accordingly.
Parallel Extensions has cancellation built into it all over the place (and in particular cancellation tokens to allow only appropriate pieces of code to cancel the task). It's all still co-operative though, as far as I'm aware. Forced cancellation will always run the risk of corrupting state.
You can use System.ComponentModel.BackgroundWorker. It supports cancellation so that you don't have to write the boilerplate code. Client code can simply call CancelAsyn method and you can check CancellationPending property within your code when and where it makes sense to determine if the client has cancelled the operation and bail out.
As Jon has already stated, you can't tell a thread: "please stop now" and expect the thread to obey. Thread.Abort will not tell it to stop, will simply "unplug" it. :)
What I did in the past was add a series of "if (wehavetostop)" within the thread's code and if the user pressed a "cancel" I put wehavetostop == true.
It's not too elegant and in some cases it may be "hard" to put the "if" checks, specially if your thread runs a "long" operation that you can't divide.
If you are trying to establish a network connection (and it's taking time) and you really think that an "abnormal" termination of the thread wouldn't cause any corrupting state, you may use it, but remember that you cannot trust the state of things that were involved in that thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With