

Store references to the tasks so that we can wait on them and Var tokenSource = new CancellationTokenSource() In this case, you must use a try/catch block to handle the exceptions on the calling thread. It also shows that when a user delegate terminates by throwing a TaskCanceledException, the calling thread can optionally use the Wait method or WaitAll method to wait for the tasks to finish.
TASKFACTORY AS TIMER HOW TO
This example shows how to terminate a Task and its children in response to a cancellation request. If cancellation is requested before the task runs, then the user delegate is never executed and the task object transitions into the Canceled state. If the task is already running, it is up to the user delegate to notice the request and respond appropriately. The calling thread does not forcibly end the task it only signals that cancellation is requested. Optionally notice on the calling thread that the task was canceled. Notice and respond to the cancellation request in your user delegate. Pass a cancellation token to your user delegate and optionally to the task instance. So passing the CancellationToken in the constructor is only usefull if you use the TaskFactory.These examples show how to perform the following tasks: M_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask), ref stackMark) Īs you can see m_defaultCancellationToken from the constructor is used when you're not using the one with the CancellationToken parameter. Return Task.StartNew(currTask, function, m_defaultCancellationToken, StackCrawlMark stackMark = StackCrawlMark.LookForM圜aller Here is an overload without the CancellationToken parameter and how it's implemented: // Methods containing StackCrawlMark local var have to be marked non-inlineable M_defaultContinuationOptions = continuationOptions

M_defaultCreationOptions = creationOptions M_defaultCancellationToken = cancellationToken After all, if I'm going to explicitly pass the token to StartNew(), then what is the point of passing it to the TaskFactory constructor?Ĭonstructor: public TaskFactory(CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)ĬheckMultiTaskContinuationOptions(continuationOptions) So I'm not sure if I'm just seeing examples that are doing work they don't need to do, or if there is something fundamental I'm misunderstanding here. Passing the same exact token to StartNew() as the factory is allegedly configured to automatically associate with the tasks created by this factory. So that's what I thought, except I see a number of examples online that essentially look like this: TaskFactory factory = new TaskFactory(token) Of course why would I do that? It would seem to make more sense to just use the () for the one-off task (unless I had other common config I guess). now create a special task that marches to its own drummer (token)įactory.StartNew(() => DoSomeWork(differentToken), differentToken) imagine a few more tasks created just like above This leads me to believe that I can do this: TaskFactory factory = new TaskFactory(token) įactory.StartNew(() => DoSomeWork(token)) // don't need to pass token to StartNew() TaskFactory unless another CancellationToken is explicitly specified The CancellationToken that will be assigned to tasks created by this When using a TaskFactory (default constructor) and you want your tasks cancelable then you do the typical: (() => DoSomeWork(token), token) įactory.StartNew(() => DoSomeWork(token), token) Īccording to MSDN if instead of TaskFactory() you use TaskFactory(CancellationToken) then:
