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?
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.
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!
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With