JoinableTaskFactory.Run 方法

定义

重载

Run(Func<Task>)

运行指定的异步方法以完成,同时同步阻止调用线程。

Run(Func<Task>, JoinableTaskCreationOptions)

运行指定的异步方法以完成,同时同步阻止调用线程。

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

运行指定的异步方法以完成,同时同步阻止调用线程。

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

运行指定的异步方法以完成,同时同步阻止调用线程。

Run(Func<Task>)

运行指定的异步方法以完成,同时同步阻止调用线程。

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))

参数

asyncMethod
Func<Task>

要执行的异步方法。

注解

委托引发的任何异常都将在其原始类型中重新引发给此方法的调用方。

当委托从生成 await 恢复时,默认行为是恢复其原始上下文,就像普通异步方法执行一样。 例如,如果调用方位于main线程上,则执行将在main线程上的 await 之后恢复;但如果在线程池线程上启动,则执行将在线程池线程上恢复。

// 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).
});

适用于

Run(Func<Task>, JoinableTaskCreationOptions)

运行指定的异步方法以完成,同时同步阻止调用线程。

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)

参数

asyncMethod
Func<Task>

要执行的异步方法。

creationOptions
JoinableTaskCreationOptions

用于自定义任务的行为的 JoinableTaskCreationOptions

适用于

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

运行指定的异步方法以完成,同时同步阻止调用线程。

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

类型参数

T

由异步操作返回的值的类型。

参数

asyncMethod
Func<Task<T>>

要执行的异步方法。

creationOptions
JoinableTaskCreationOptions

用于自定义任务的行为的 JoinableTaskCreationOptions

返回

T

返回 asyncMethod的任务的结果。

注解

委托引发的任何异常都将在其原始类型中重新引发给此方法的调用方。

当委托从生成 await 恢复时,默认行为是恢复其原始上下文,就像普通异步方法执行一样。 例如,如果调用方位于main线程上,则执行将在main线程上的 await 之后恢复;但如果在线程池线程上启动,则执行将在线程池线程上恢复。

// 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).
});

适用于

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

运行指定的异步方法以完成,同时同步阻止调用线程。

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

类型参数

T

由异步操作返回的值的类型。

参数

asyncMethod
Func<Task<T>>

要执行的异步方法。

返回

T

返回 asyncMethod的任务的结果。

注解

委托引发的任何异常都将在其原始类型中重新引发给此方法的调用方。

当委托从生成 await 恢复时,默认行为是恢复其原始上下文,就像普通异步方法执行一样。 例如,如果调用方位于main线程上,则执行将在main线程上的 await 之后恢复;但如果在线程池线程上启动,则执行将在线程池线程上恢复。

有关示例, Run(Func<Task>) 请参阅重载文档。

适用于