Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I cancel methods through delegates in C#

Tags:

c#

delegates

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 :)

like image 349
Marc Vitalis Avatar asked Sep 11 '09 10:09

Marc Vitalis


People also ask

Why delegates why not call methods directly?

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!

What will happen if a delegate has a non void return type?

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.

Why use delegates over methods?

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.

What is public delegate void?

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.


3 Answers

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.

like image 86
Jon Skeet Avatar answered Sep 20 '22 00:09

Jon Skeet


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.

like image 37
Mehmet Aras Avatar answered Sep 18 '22 00:09

Mehmet Aras


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.

like image 26
Martin Marconcini Avatar answered Sep 20 '22 00:09

Martin Marconcini