JoinableTaskFactory.Run 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| 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))
參數
備註
代理拋出的任何例外都會以原始型別重新拋給該方法的呼叫者。
當代理從 yield wait 恢復時,預設行為是像一般非同步方法執行一樣,在原始上下文中繼續。 例如,若呼叫者在主執行緒,執行會在主執行緒等待後繼續;但如果是從 threadpool 執行緒開始,則會在 threadpool 執行緒上繼續。
// 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)
參數
- 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
非同步操作回傳的值類型。
參數
- creationOptions
- JoinableTaskCreationOptions
它們 JoinableTaskCreationOptions 用來自訂任務的行為。
傳回
任務結果由 返回 asyncMethod。
備註
代理拋出的任何例外都會以原始型別重新拋給該方法的呼叫者。
當代理從 yield wait 恢復時,預設行為是像一般非同步方法執行一樣,在原始上下文中繼續。 例如,若呼叫者在主執行緒,執行會在主執行緒等待後繼續;但如果是從 threadpool 執行緒開始,則會在 threadpool 執行緒上繼續。
// 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。
備註
代理拋出的任何例外都會以原始型別重新拋給該方法的呼叫者。
當代理從 yield wait 恢復時,預設行為是像一般非同步方法執行一樣,在原始上下文中繼續。 例如,若呼叫者在主執行緒,執行會在主執行緒等待後繼續;但如果是從 threadpool 執行緒開始,則會在 threadpool 執行緒上繼續。
請參考 Run(Func<Task>) 過載文件中的範例。