Task.RunSynchronously 方法

定义

对当前的 Task 同步运行 TaskScheduler

重载

RunSynchronously(TaskScheduler)

对提供的 Task 同步运行 TaskScheduler

RunSynchronously()

对当前的 Task 同步运行 TaskScheduler

RunSynchronously(TaskScheduler)

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

对提供的 Task 同步运行 TaskScheduler

public:
 void RunSynchronously(System::Threading::Tasks::TaskScheduler ^ scheduler);
public void RunSynchronously (System.Threading.Tasks.TaskScheduler scheduler);
member this.RunSynchronously : System.Threading.Tasks.TaskScheduler -> unit
Public Sub RunSynchronously (scheduler As TaskScheduler)

参数

scheduler
TaskScheduler

尝试对其以内联方式运行此任务的计划程序。

例外

已释放了 Task 实例。

scheduler 参数为 null

Task 并非要启动的有效状态。 它可能已启动、执行或取消,或者它可能是以不支持直接计划的方式创建的。

注解

通过调用 RunSynchronously 方法执行的任务通过调用 TaskTask<TResult> 类构造函数进行实例化。 要同步运行的任务必须处于 状态 Created 。 任务只能启动并运行一次。 第二次计划任务的任何尝试都会导致异常。

如果目标计划程序不支持在当前线程上运行此任务,则将在计划程序上安排任务执行,并且当前线程将阻塞,直到任务完成执行。 因此,调用线程不需要调用 等 Wait 方法来确保任务已完成执行。 有关任务操作异常处理的详细信息,请参阅 异常处理

另请参阅

适用于

RunSynchronously()

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

对当前的 Task 同步运行 TaskScheduler

public:
 void RunSynchronously();
public void RunSynchronously ();
member this.RunSynchronously : unit -> unit
Public Sub RunSynchronously ()

例外

已释放了 Task 实例。

Task 并非要启动的有效状态。 它可能已启动、执行或取消,或者它可能是以不支持直接计划的方式创建的。

示例

以下示例将调用 RunSynchronously 方法执行的任务与异步执行的任务进行比较。 在这两种情况下,任务执行相同的 lambda 表达式,这些表达式显示任务 ID 和运行任务的线程的 ID。 任务计算 1 到 1,000,000 之间的整数之和。 如示例输出所示,通过调用 RunSynchronously 方法执行的任务在应用程序线程上运行,而异步任务则不运行。

using System;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      Console.WriteLine("Application executing on thread {0}",
                        Thread.CurrentThread.ManagedThreadId);
      var asyncTask = Task.Run( () => {  Console.WriteLine("Task {0} (asyncTask) executing on Thread {1}",
                                                           Task.CurrentId,
                                                           Thread.CurrentThread.ManagedThreadId);
                                         long sum = 0;
                                         for (int ctr = 1; ctr <= 1000000; ctr++ )
                                            sum += ctr;
                                         return sum;
                                      });
      var syncTask = new Task<long>( () =>  { Console.WriteLine("Task {0} (syncTask) executing on Thread {1}",
                                                                 Task.CurrentId,
                                                                 Thread.CurrentThread.ManagedThreadId);
                                              long sum = 0;
                                              for (int ctr = 1; ctr <= 1000000; ctr++ )
                                                 sum += ctr;
                                              return sum;
                                            });
      syncTask.RunSynchronously();
      Console.WriteLine();
      Console.WriteLine("Task {0} returned {1:N0}", syncTask.Id, syncTask.Result);
      Console.WriteLine("Task {0} returned {1:N0}", asyncTask.Id, asyncTask.Result);
   }
}
// The example displays the following output:
//       Application executing on thread 1
//       Task 1 (syncTask) executing on Thread 1
//       Task 2 (asyncTask) executing on Thread 3
//       1 status: RanToCompletion
//       2 status: RanToCompletion
//
//       Task 2 returned 500,000,500,000
//       Task 1 returned 500,000,500,000
open System
open System.Threading
open System.Threading.Tasks

printfn $"Application executing on thread {Thread.CurrentThread.ManagedThreadId}"

let asyncTask =
    Task.Run(fun () ->
        printfn $"Task {Task.CurrentId} (asyncTask) executing on Thread {Thread.CurrentThread.ManagedThreadId}"
        let mutable sum = 0L

        for i = 1 to 1000000 do
            sum <- sum + int64 i

        sum)

let syncTask =
    new Task<int64>(fun () ->
        printfn $"Task {Task.CurrentId} (syncTask) executing on Thread {Thread.CurrentThread.ManagedThreadId}"
        let mutable sum = 0L

        for i = 1 to 1000000 do
            sum <- sum + int64 i

        sum)

syncTask.RunSynchronously()
printfn $"\nTask {syncTask.Id} returned {syncTask.Result:N0}"
printfn $"Task {asyncTask.Id} returned {asyncTask.Result:N0}"

// The example displays the following output:
//       Application executing on thread 1
//       Task 1 (syncTask) executing on Thread 1
//       Task 2 (asyncTask) executing on Thread 3
//       1 status: RanToCompletion
//       2 status: RanToCompletion
//
//       Task 2 returned 500,000,500,000
//       Task 1 returned 500,000,500,000
Imports System.Threading
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Console.WriteLine("Application executing on thread {0}",
                        Thread.CurrentThread.ManagedThreadId)
      Dim asyncTask = Task.Run( Function()
                                   Console.WriteLine("Task {0} (asyncTask) executing on Thread {1}",
                                                     Task.CurrentId,
                                                     Thread.CurrentThread.ManagedThreadId)
                                   Dim sum As Long = 0
                                   For ctr As Integer = 1 To 1000000
                                      sum += ctr
                                   Next
                                   Return sum
                                End Function)
      Dim syncTask As New Task(Of Long)( Function()
                                            Console.WriteLine("Task {0} (syncTask) executing on Thread {1}",
                                                              Task.CurrentId,
                                                              Thread.CurrentThread.ManagedThreadId)
                                            Dim sum As Long = 0
                                            For ctr As Integer = 1 To 1000000
                                               sum += ctr
                                            Next
                                            Return sum
                                         End Function)
      syncTask.RunSynchronously()
      Console.WriteLine()
      Console.WriteLine("Task {0} returned {1:N0}", syncTask.Id, syncTask.Result)
      Console.WriteLine("Task {0} returned {1:N0}", asyncTask.Id, asyncTask.Result)
   End Sub
End Module
' The example displays the following output:
'       Application executing on thread 1
'       Task 1 (syncTask) executing on Thread 1
'       Task 2 (asyncTask) executing on Thread 3
'       1 status: RanToCompletion
'       2 status: RanToCompletion
'
'       Task 2 returned 500,000,500,000
'       Task 1 returned 500,000,500,000

注解

通常,任务在线程池线程上异步执行,并且不会阻止调用线程。 通过调用 RunSynchronously() 方法执行的任务与当前 TaskScheduler 相关联,并在调用线程上运行。 如果目标计划程序不支持在调用线程上运行此任务,则将在计划程序上安排任务执行,调用线程将阻塞,直到任务完成执行。 即使任务同步运行,调用线程仍应调用 Wait 来处理任务可能引发的任何异常。 有关异常处理的详细信息,请参阅 异常处理

通过调用 RunSynchronously 方法执行的任务通过调用 TaskTask<TResult> 类构造函数进行实例化。 要同步运行的任务必须处于 状态 Created 。 任务只能启动并运行一次。 第二次计划任务的任何尝试都会导致异常。

另请参阅

适用于