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 句柄。
程序集: mscorlib(位于 mscorlib.dll)
名称 | 说明 | |
---|---|---|
![]() ![]() | Run |
将在线程池上运行的指定工作排队,并返回代表该工作的 Task 对象。 |
![]() ![]() | Run |
将在线程池上运行的指定工作排队,并返回代表该工作的 Task 对象。 可使用取消标记来取消工作。 |
![]() ![]() | Run |
将在线程池上运行的指定工作排队,并返回 function 所返回的任务的代理。 |
![]() ![]() | Run |
将在线程池上运行的指定工作排队,并返回 function 所返回的任务的代理项。 |
![]() ![]() | Run |
将在线程池上运行的指定工作排队,并返回代表该工作的 Task 对象。 |
![]() ![]() | Run |
将在线程池上运行的指定工作排队,并返回代表该工作的 Task(TResult) 对象。 借助取消标记,可取消工作。 |
![]() ![]() | Run |
将指定的工作排成队列在线程池上运行,并返回由 function 返回的 Task(TResult) 的代理。 |
![]() ![]() | Run |
将指定的工作排成队列在线程池上运行,并返回由 function 返回的 Task(TResult) 的代理。 |