TaskRun 方法

来源:互联网 发布:java list remove方法 编辑:程序博客网 时间:2024/06/01 07:25
TaskRun 方法
 

将在线程池上运行的指定工作排队,并返回代表该工作的 Task 对象。

命名空间:   System.Threading.Tasks
程序集:  mscorlib(位于 mscorlib.dll)

语法
public static Task Run( 
  Action action 
)
public
static Task^ Run( 
    Action^ action 
)
static member Run :         action:Action -> Task
Public Shared Function Run (action As Action) As Task

参数

action
Type:

以异步方式执行的工作量。

返回值

Type:

表示在线程池执行的队列的任务。

异常

ExceptionConditionArgumentNullException

   action 参数是 null

备注

M:系统线程。任务。任务。运行(系统。行动)方法允许您创建和执行一个任务调用一个方法,是一个简单的选择:M系统。线程。任务。taskfactory。全面启动(系统。行动)的方法。它用以下默认值创建任务:
取消标记:System.Threading.CancellationToken.None。
P:system.threading.tasks.task.creationoptions属性值为F:System.Threading.Tasks.TaskCreationOptions.DenyChildAttach。
它使用默认任务调度器。
有关处理任务操作抛出的异常的信息,请参见异常处理(任务并行库)。


下面的示例定义了一个showthreadinfo方法显示P:system.threading.thread.managedthreadidof当前线程。它是从应用程序线程直接调用,并从T称:system.action委托传递给我:系统线程。任务。任务。运行(系统。行动)的方法。

using System; 
using System.Threading; 
using System.Threading.Tasks; 
 
public class Example 

   public static void Main() 
   { 
      ShowThreadInfo("Application"); 
 
      var t = Task.Run(() => ShowThreadInfo("Task") ); 
      t.Wait(); 
   } 
 
   static void ShowThreadInfo(String s) 
   { 
      Console.WriteLine("{0} Thread ID: {1}"
                        s, Thread.CurrentThread.ManagedThreadId); 
   } 

// The example displays the following output: 
//       Application thread ID: 1 
//       Task thread ID: 3

Imports System.ThreadingImports System.Threading.TasksModule Example   Public Sub Main()      ShowThreadInfo("Application")      Dim t As Task = Task.Run(Sub() ShowThreadInfo("Task") )      t.Wait()   End Sub   Private Sub ShowThreadInfo(s As String)      Console.WriteLine("{0} Thread ID: {1}",                        s, Thread.CurrentThread.ManagedThreadId)   End SubEnd Module' The example displays output like the following:'    Application thread ID: 1'    Task thread ID: 3

下面的示例与前一个示例相似,但它使用lambda表达式来定义任务要执行的代码。

using System; 
using System.Threading; 
using System.Threading.Tasks; 
 
public class Example 

   public static void Main() 
   { 
      Console.WriteLine("Application thread ID: {0}"
                        Thread.CurrentThread.ManagedThreadId); 
      var t = Task.Run(() => {  Console.WriteLine("Task thread ID: {0}"
                                   Thread.CurrentThread.ManagedThreadId); 
                             } ); 
      t.Wait(); 
   } 

// The example displays the following output: 
//       Application thread ID: 1 
//       Task thread ID: 3
Imports System.ThreadingImports System.Threading.TasksModule Example   Public Sub Main()      Console.WriteLine("Application thread ID: {0}",                        Thread.CurrentThread.ManagedThreadId)      Dim t As Task = Task.Run(Sub()                                  Console.WriteLine("Task thread ID: {0}",                                                    Thread.CurrentThread.ManagedThreadId)                               End Sub)      t.Wait()   End SubEnd Module' The example displays output like the following:'    Application thread ID: 1'    Task thread ID: 3
示例说明异步任务在与主应用程序线程不同的线程上执行。
打电话给我:system.threading.tasks.task.wait方法确保任务完成和显示输出应用程序结束之前。否则,main方法在任务完成前将完成。
下面的示例演示了m。它定义了一个目录名数组,并启动一个单独的任务来检索每个目录中的文件名。所有的任务写的文件名,单T:系统。收藏。同时concurrentbag ` 1对象。然后示例调用M系统。线程。任务。任务。WaitAll(系统线程。任务。任务[ ])以确保所有任务都完成的方法,然后显示计数的文件名写入到T的总数为:系统。收藏。同时concurrentbag ` 1对象。


using System; 
using System.Collections.Concurrent; 
using System.Collections.Generic; 
using System.IO; 
using System.Threading.Tasks; 
 
public class Example 

   public static void Main() 
   { 
      var list = new ConcurrentBag<string>(); 
      string[] dirNames = { "."".." }; 
      List<Task> tasks = new List<Task>(); 
      foreach (var dirName in dirNames) { 
         Task t = Task.Run( () => { foreach(var path in Directory.GetFiles(dirName))  
                                       list.Add(path); }  ); 
         tasks.Add(t); 
      } 
      Task.WaitAll(tasks.ToArray()); 
      foreach (Task t in tasks) 
         Console.WriteLine("Task {0} Status: {1}", t.Id, t.Status); 
 
      Console.WriteLine("Number of files read: {0}", list.Count); 
   } 

// The example displays output like the following: 
//       Task 1 Status: RanToCompletion 
//       Task 2 Status: RanToCompletion 
//       Number of files read: 23
Imports System.Collections.ConcurrentImports System.Collections.GenericImports System.IOImports System.Threading.TasksModule Example   Public Sub Main()      Dim list As New ConcurrentBag(Of String)()      Dim dirNames() As String = { ".", ".." }      Dim tasks As New List(Of Task)()      For Each dirName In dirNames          Dim t As Task = Task.Run( Sub()                                      For Each path In Directory.GetFiles(dirName)                                          list.Add(path)                                      Next                                   End Sub  )         tasks.Add(t)      Next      Task.WaitAll(tasks.ToArray())      For Each t In tasks         Console.WriteLine("Task {0} Status: {1}", t.Id, t.Status)      Next         Console.WriteLine("Number of files read: {0}", list.Count)   End SubEnd Module' The example displays output like the following:'       Task 1 Status: RanToCompletion'       Task 2 Status: RanToCompletion'       Number of files read: 23
版本信息:

通用 Windows 平台
自 8 起可用
.NET Framework
自 4.5 起可用
可移植类库
在 可移植 .NET 平台 中受支持
Windows Phone Silverlight
自 8.0 起可用
Windows Phone
自 8.1 起可用

TaskRun 方法

将在线程池上运行的指定工作排队,并返回该工作的任务或 Task 句柄。

命名空间:   System.Threading.Tasks
程序集:  mscorlib(位于 mscorlib.dll)

重载列表
 名称说明System_CAPS_pubmethod System_CAPS_staticRun

将在线程池上运行的指定工作排队,并返回代表该工作的 Task 对象。

System_CAPS_pubmethod System_CAPS_staticRun

将在线程池上运行的指定工作排队,并返回代表该工作的 Task 对象。 可使用取消标记来取消工作。

System_CAPS_pubmethod System_CAPS_staticRun

将在线程池上运行的指定工作排队,并返回 function 所返回的任务的代理。

System_CAPS_pubmethod System_CAPS_staticRun

将在线程池上运行的指定工作排队,并返回 function 所返回的任务的代理项。

System_CAPS_pubmethod System_CAPS_staticRun

将在线程池上运行的指定工作排队,并返回代表该工作的 Task 对象。

System_CAPS_pubmethod System_CAPS_staticRun

将在线程池上运行的指定工作排队,并返回代表该工作的 Task(TResult) 对象。 借助取消标记,可取消工作。

System_CAPS_pubmethod System_CAPS_staticRun

将指定的工作排成队列在线程池上运行,并返回由 function 返回的 Task(TResult) 的代理。

System_CAPS_pubmethod System_CAPS_staticRun

将指定的工作排成队列在线程池上运行,并返回由 function 返回的 Task(TResult) 的代理。

备注

Run 方法提供了一组方便地开始一项任务使用默认值的重载。 它是一个轻型替代方法 StartNew 重载。

另请参阅

Task 类
System.Threading.Tasks 命名空间










原创粉丝点击