Windows apps
Collapse the table of content
Expand the table of content

Task.RunSynchronously Method (TaskScheduler)

 

Runs the Task synchronously on the TaskScheduler provided.

Namespace:   System.Threading.Tasks
Assembly:  mscorlib (in mscorlib.dll)

public void RunSynchronously(
	TaskScheduler scheduler
)

Parameters

scheduler
Type: System.Threading.Tasks.TaskScheduler

The scheduler on which to attempt to run this task inline.

Exception Condition
ObjectDisposedException

The Task instance has been disposed.

ArgumentNullException

The scheduler argument is null.

InvalidOperationException

The Task is not in a valid state to be started. It may have already been started, executed, or canceled, or it may have been created in a manner that doesn't support direct scheduling.

Tasks executed by calling the RunSynchronously method are instantiated by calling a Task or Task<TResult> class constructor. The task to be run synchronously must be in the TaskStatus.Created state. A task may be started and run only once. Any attempts to schedule a task a second time results in an exception.

If the target scheduler does not support running this task on the current thread, the task will be scheduled for execution on the scheduler, and the current thread will block until the task has completed execution. Because of this, the calling thread does not need to call a method such as Wait to ensure that the task has completed execution. For more information on exception handling for task operations, see Exception Handling (Task Parallel Library).

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 5.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2016 Microsoft