Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Task.Factory.StartNew() Taskscheduler parameter

Tags:

c#

.net-4.0

see: TaskFactory

When i want to make a task to be long-running and also to be cancellable, If i am calling this method from ui, how do i pass the taskscheduler parameter?

like image 679
Benny Avatar asked Jul 24 '10 07:07

Benny


People also ask

What does task factory StartNew do?

StartNew(Action<Object>, Object, CancellationToken, TaskCreationOptions, TaskScheduler) Creates and starts a task for the specified action delegate, state, cancellation token, creation options and task scheduler.

What is the difference between task run and task factory StartNew?

Run(action) internally uses the default TaskScheduler , which means it always offloads a task to the thread pool. StartNew(action) , on the other hand, uses the scheduler of the current thread which may not use thread pool at all!

What is the use of task factory in C#?

The TaskFactory class allows you to do the following: Create a task and start it immediately by calling the StartNew method. Starting with . NET Framework 4.5, the Task.

What is task factory?

Task Factory simplifies component loading and data warehouse management. With Task Factory, you can connect to nearly any data source, which saves development time, especially when you're working with data warehouse scenarios with multiple disparate data sources.


1 Answers

It's not really obvious what the problem is. Why can't you just call:

CancellationToken token = new CancellationToken(false);
TaskScheduler scheduler = TaskScheduler.Default;
Task task = taskFactory.StartNew(action, token, 
                                 TaskCreationOptions.LongRunning, scheduler);
like image 120
Jon Skeet Avatar answered Oct 14 '22 02:10

Jon Skeet