Non-commercial cancellation of an asynchronous operation

In my Windows Form application, I need to call a long running third party math library (Accord.NET). During computation, I want my GUI to be responsive and also give the user the ability to undo a long running operation.

Is there a way to execute this function on a background thread and give the user the option to cancel it? Note that the long-running function I want to call is in an external library and does not take a value CancellationToken

(otherwise I could easily use ThreadPool

QueueUserWorkItem

for example). Therefore, I am looking for a way to immediately cancel the operation.

+3


source to share


2 answers


I would say you have three options.

One, run the computation on your own thread. This will make the user interface responsive. Instead of a Cancel button, simply inform the user that the action to be taken cannot be stopped once viewed.

Two, run the computation on your own thread as above; however, pretends to have canceled it by simply decreasing the thread priority. You can let the calculation keep going, but just stop reporting progress on your main thread. When it's done, just throw away the results.



Three, put your own appdomain / process in it. Delete this external process if desired.


Each of them has good and bad things.

+3


source


and? Just use Thread and working method.



Thread thread = new Thread(MyWorkerMethod);
thread.Start();

thread.Abort();


void MyWorkerMethod() {
    // do anything you want in here
}

      

+1


source







All Articles