语言

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>

要执行的异步方法。

注解

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

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

// 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的任务的结果。

注解

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

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

// 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的任务的结果。

注解

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

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

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

适用于