Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to end a System.Threading.Tasks.Task?

As per the title really.

I have a long running task that polls a message queue.

Occasionally I may wish to stop the task, do some work and then start it again. What is the correct way of doing this?

like image 853
Ben Foster Avatar asked Jul 17 '11 11:07

Ben Foster


People also ask

What is Task in threading?

A task is something you want done. A thread is one of the many possible workers which performs that task. In . NET 4.0 terms, a Task represents an asynchronous operation. Thread(s) are used to complete that operation by breaking the work up into chunks and assigning to separate threads.

Which threading Task method will create a Task that will complete when any of the associated tasks have completed?

Wait method. A call to the Wait method blocks the calling thread until the single class instance has completed execution. The following example calls the parameterless Wait() method to wait unconditionally until a task completes. The task simulates work by calling the Thread.


1 Answers

The proper way of stooping tasks is using cancellation tokens.

like image 135
Darin Dimitrov Avatar answered Nov 04 '22 05:11

Darin Dimitrov