JoinableTaskFactory.Run Method

Definition

Overloads

Run(Func<Task>)

Runs the specified asynchronous method to completion while synchronously blocking the calling thread.

Run(Func<Task>, JoinableTaskCreationOptions)

Runs the specified asynchronous method to completion while synchronously blocking the calling thread.

Run<T>(Func<Task<T>>, JoinableTaskCreationOptions)

Runs the specified asynchronous method to completion while synchronously blocking the calling thread.

Run<T>(Func<Task<T>>)

Runs the specified asynchronous method to completion while synchronously blocking the calling thread.

Run(Func<Task>)

Runs the specified asynchronous method to completion while synchronously blocking the calling thread.

public:
 void Run(Func<System::Threading::Tasks::Task ^> ^ asyncMethod);
public void Run (Func<System.Threading.Tasks.Task> asyncMethod);
member this.Run : Func<System.Threading.Tasks.Task> -> unit
Public Sub Run (asyncMethod As Func(Of Task))

Parameters

asyncMethod
Func<Task>

The asynchronous method to execute.

Remarks

Any exception thrown by the delegate is rethrown in its original type to the caller of this method.

When the delegate resumes from a yielding await, the default behavior is to resume in its original context as an ordinary async method execution would. For example, if the caller was on the main thread, execution resumes after an await on the main thread; but if it started on a threadpool thread it resumes on a threadpool thread.

// On threadpool or Main thread, this method will block
// the calling thread until all async operations in the
// delegate complete.
joinableTaskFactory.Run(async delegate {
    // still on the threadpool or Main thread as before.
    await OperationAsync();
    // still on the threadpool or Main thread as before.
    await Task.Run(async delegate {
         // Now we're on a threadpool thread.
         await Task.Yield();
         // still on a threadpool thread.
    });
    // Now back on the Main thread (or threadpool thread if that's where we started).
});

Applies to

Run(Func<Task>, JoinableTaskCreationOptions)

Runs the specified asynchronous method to completion while synchronously blocking the calling thread.

public:
 void Run(Func<System::Threading::Tasks::Task ^> ^ asyncMethod, Microsoft::VisualStudio::Threading::JoinableTaskCreationOptions creationOptions);
public void Run (Func<System.Threading.Tasks.Task> asyncMethod, Microsoft.VisualStudio.Threading.JoinableTaskCreationOptions creationOptions);
member this.Run : Func<System.Threading.Tasks.Task> * Microsoft.VisualStudio.Threading.JoinableTaskCreationOptions -> unit
Public Sub Run (asyncMethod As Func(Of Task), creationOptions As JoinableTaskCreationOptions)

Parameters

asyncMethod
Func<Task>

The asynchronous method to execute.

creationOptions
JoinableTaskCreationOptions

The JoinableTaskCreationOptions used to customize the task's behavior.

Applies to

Run<T>(Func<Task<T>>, JoinableTaskCreationOptions)

Runs the specified asynchronous method to completion while synchronously blocking the calling thread.

public:
generic <typename T>
 T Run(Func<System::Threading::Tasks::Task<T> ^> ^ asyncMethod, Microsoft::VisualStudio::Threading::JoinableTaskCreationOptions creationOptions);
public T Run<T> (Func<System.Threading.Tasks.Task<T>> asyncMethod, Microsoft.VisualStudio.Threading.JoinableTaskCreationOptions creationOptions);
member this.Run : Func<System.Threading.Tasks.Task<'T>> * Microsoft.VisualStudio.Threading.JoinableTaskCreationOptions -> 'T
Public Function Run(Of T) (asyncMethod As Func(Of Task(Of T)), creationOptions As JoinableTaskCreationOptions) As T

Type Parameters

T

The type of value returned by the asynchronous operation.

Parameters

asyncMethod
Func<Task<T>>

The asynchronous method to execute.

creationOptions
JoinableTaskCreationOptions

The JoinableTaskCreationOptions used to customize the task's behavior.

Returns

T

The result of the Task returned by asyncMethod.

Remarks

Any exception thrown by the delegate is rethrown in its original type to the caller of this method.

When the delegate resumes from a yielding await, the default behavior is to resume in its original context as an ordinary async method execution would. For example, if the caller was on the main thread, execution resumes after an await on the main thread; but if it started on a threadpool thread it resumes on a threadpool thread.

// On threadpool or Main thread, this method will block
// the calling thread until all async operations in the
// delegate complete.
joinableTaskFactory.Run(async delegate {
    // still on the threadpool or Main thread as before.
    await OperationAsync();
    // still on the threadpool or Main thread as before.
    await Task.Run(async delegate {
         // Now we're on a threadpool thread.
         await Task.Yield();
         // still on a threadpool thread.
    });
    // Now back on the Main thread (or threadpool thread if that's where we started).
});

Applies to

Run<T>(Func<Task<T>>)

Runs the specified asynchronous method to completion while synchronously blocking the calling thread.

public:
generic <typename T>
 T Run(Func<System::Threading::Tasks::Task<T> ^> ^ asyncMethod);
public T Run<T> (Func<System.Threading.Tasks.Task<T>> asyncMethod);
member this.Run : Func<System.Threading.Tasks.Task<'T>> -> 'T
Public Function Run(Of T) (asyncMethod As Func(Of Task(Of T))) As T

Type Parameters

T

The type of value returned by the asynchronous operation.

Parameters

asyncMethod
Func<Task<T>>

The asynchronous method to execute.

Returns

T

The result of the Task returned by asyncMethod.

Remarks

Any exception thrown by the delegate is rethrown in its original type to the caller of this method.

When the delegate resumes from a yielding await, the default behavior is to resume in its original context as an ordinary async method execution would. For example, if the caller was on the main thread, execution resumes after an await on the main thread; but if it started on a threadpool thread it resumes on a threadpool thread.

See the Run(Func<Task>) overload documentation for an example.

Applies to