tags:

views:

149

answers:

1

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?

+1  A: 

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);
Jon Skeet
i have revised my question, is this safe to pass .Default when i am calling this method from ui thread? considering this is a long running and blocking task.
Benny
@Benny: Your question itself still isn't very clear, as it doesn't mention TaskScheduler.Default. But yes, TaskScheduler.Default is fine to specify from the UI thread - but the task itself won't *run* on the UI thread.
Jon Skeet
@Jon, sorry, I just want to make sure when i specify TaskScheduler.Default from UI thread, the task won't block the UI thread, thanks.
Benny