khargoosh
khargoosh

Reputation: 1520

Does TryExecuteTask(task) always block?

I'm writing a FIFO queued thread-limited task scheduler.

Does a call to TaskScheduler.TryExecuteTask(task) always block while task is executing? Testing so far indicates that this is the case, however I can't find this documented anywhere.

Is there ever any reason to call task.Wait() following the above call? Doing this results in an AggregateException if the task ends in a Faulted state.

Upvotes: 2

Views: 753

Answers (1)

Peter Keuter
Peter Keuter

Reputation: 788

A call to TryExecuteTask will always block, it runs the task you are providing it on the scheduler on which you are calling the function and returns if the Task has finished successfully.

If you want the task to run in a different thread: on this page there is an example on how to use QueueUserWorkItem to inform the ThreadPool that there's work to be executed. When you call the TryExecuteTask inside there, an available thread will pick it up and executes the task.

task.Wait() is generally used when passing the task to a scheduler to block until the task has completed. Since you are not executing your task in a different thread, the code blocks anyway and calling task.Wait() would be redundant.

Upvotes: 1

Related Questions