1

I just discovered the async keyword in C# and just wondering how to stop it using a button click. I just want to understand how it works and how to stop the task after you execute another event. I tried stop it using bool but had no luck. Below is my code:

//start private void button1_Click(object sender, EventArgs e) { Read(true); } private async void Read(bool state) { while (state == true) { await Task.Delay(4000); listBox1.Items.Insert(0, "Read 1"); } } //stop private void button2_Click(object sender, EventArgs e) { Read(false); } 
2

3 Answers 3

8

You can try something like this

private CancellationTokenSource cancellationToken; // start private void button1_Click(object sender, EventArgs e) { cancellationToken= new CancellationTokenSource(); Read(); } private async void Read() { Task.Factory.StartNew(() => { listBox1.Items.Insert(0, "Read 1"); }, cancellationToken.Token); } // stop private void button2_Click(object sender, EventArgs e) { if(cancellationToken!= null) cancellationToken.Cancel(); } 
Sign up to request clarification or add additional context in comments.

3 Comments

Its says The 'await' operator can only be used within an async lambda expression.
Sorry, updated! Probably you will need to find another way to simulate the delay
For me this did not work without handling the cancellation button by myself the way Sasha answered here stackoverflow.com/questions/15067865/… So instead of filling the Listbox I used a function containing the question if(cancellationToken.IsCancellationRequested) {return}
-1

i think if you want the thread to be stopped in some cases you may do these

 private void button1_Click(object sender, EventArgs e) { var tokenSource2 = new CancellationTokenSource(); CancellationToken ct = tokenSource2.Token; Read(ct); } private async void Read( CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { Thread.Sleep(4000); } } 

1 Comment

So how do you cancel it?
-1

This is the way it worked to me. I used a progressBar to see my async task is still running. The task really runs async to the GUI Task. I can enter values in my richTextBox, click buttons or whatever, while the bar still runs on. Because of the different threads the bar treatment must be done with Invoke to avoid a cross-thread operation exception.

private CancellationTokenSource cancellationToken; private async void btnRun_Click(object sender, EventArgs e) { progressBar1.Value = 0; cancellationToken = new CancellationTokenSource(); await Task.Run(() => { workFunction(progressBar1); }, cancellationToken.Token); rtbResult.Text += "finished work function :-)\n"; } private void workFunction(ProgressBar bar) { for (int i = 0; i < 25; i++) { Thread.Sleep(500); bar.BeginInvoke(new Action(() => { bar.Value += 400; })); if(cancellationToken.IsCancellationRequested) { return; } } } private void btnCancel_Click(object sender, EventArgs e) { if(cancellationToken != null) { cancellationToken.Cancel(); } } 

The cancellation must be handeled in the work function, as Sasha said here How to use the CancellationToken without throwing/catching an exception?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.