0

I have time-consuming function and I want to give the user an opportunity to stop it by clicking a button in the UI when he notices that it takes too long. How can I do this?

3
  • msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx Commented Aug 27, 2012 at 18:02
  • 1
    You'll need to run it on a different thread, then provide some kind of signal or abort the thread. Commented Aug 27, 2012 at 18:02
  • Create a Task<T> and use Cancellation Request Commented Aug 27, 2012 at 18:05

5 Answers 5

3

You can use BackgroundWorker class to run time and resource consuming stuff on other thread, and use its CancelAsync method, to request (it's not immediate execution) cancelation of the other thread.

For concrete example on how to implement that, can have a look on accepted answer in this question:

How to wait for a BackgroundWorker to cancel?

Sign up to request clarification or add additional context in comments.

Comments

1

First of all, you need to run the time-consuming function in a thread separate from the main thread. Otherwise the UI will stop responding.

Then you need to have a static variable or a shared instance where the UI can set a flag indicating that the time-consuming function should stop.

Finally, the time-consuming function should regular check the flag and stop processing if it is set.

The BackgroundWorker class implements this pattern and solves a few other requirements as well (such as the inter-thread communication and the progress reporting).

Comments

1

Lets say your time-consuming method is called MyTimeConsumingMethod.

void MyTimeConsumingMethod() { //Do stuff } 

Put globally a thread:

Thread t; 

Put in your Form_Load()

t = new Thread(new ThreadStart(MyTimeConsumingMethod)); t.Start(); 

And on button press:

t.Abort(); 

Comments

0

Try running it on a Background Worker.

This Gives a good example of how to use it.

Then you can call

Worker.CancelAsync(); 

when the user wants to cancel the operation

Comments

0

Here's an example

bool _cancel = false; private void count() { _cancel = false; new System.Threading.Thread(delegate() { for (int i = 0; i < 100000; i++) { if (_cancel) break; Console.WriteLine(i); } }).Start(); } private void button1_Click(object sender, EventArgs e) { _cancel = true; } 

10 Comments

It's not really an option to not run it in a different thread. If it's running in the UI thread you can't click the button to cancel execution, which is the requirement.
I agree. I'm assuming the OP already has it setup to run on a different thread that's why I just gave him the example on how to modify the already existing method that he/she has in place.
Updated the post to remove the "assuming" 'variable'
Pretty sure _cancel will need to be marked volatile, or wrapped in a lock block, to ensure the proper memory barriers are added.
Have you tried it ? Because personally I don't have any issues running the above code. Also, why do you need to use a lock statement if only one thread is going to be accessing the variable.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.