TaskRun 方法

Task Run 方法
 

将在线程池上运行的指定工作排队,并返回代表该工作的 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:

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

异常

Exception Condition
ArgumentNullException

   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.Threading
Imports System.Threading.Tasks

Module 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 Sub
End 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.Threading
Imports System.Threading.Tasks

Module 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 Sub
End 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.Concurrent
Imports System.Collections.Generic
Imports System.IO
Imports System.Threading.Tasks

Module 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 Sub
End 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_static Run

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

System_CAPS_pubmethod System_CAPS_static Run

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

System_CAPS_pubmethod System_CAPS_static Run

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

System_CAPS_pubmethod System_CAPS_static Run

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

System_CAPS_pubmethod System_CAPS_static Run

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

System_CAPS_pubmethod System_CAPS_static Run

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

System_CAPS_pubmethod System_CAPS_static Run

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

System_CAPS_pubmethod System_CAPS_static Run

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

备注

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










  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值