Task 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化新的 Task。
重载
| 名称 | 说明 |
|---|---|
| Task(Action) |
使用指定的操作初始化新 Task 项。 |
| Task(Action, CancellationToken) |
Task使用指定的操作和 CancellationToken. |
| Task(Action, TaskCreationOptions) |
使用指定的操作和创建选项初始化一个新 Task 项。 |
| Task(Action<Object>, Object) |
使用指定的操作和状态初始化新 Task 项。 |
| Task(Action, CancellationToken, TaskCreationOptions) |
使用指定的操作和创建选项初始化一个新 Task 项。 |
| Task(Action<Object>, Object, CancellationToken) |
Task使用指定的操作、状态和 CancellationToken. |
| Task(Action<Object>, Object, TaskCreationOptions) |
使用指定的操作、状态和选项初始化一个新 Task 项。 |
| Task(Action<Object>, Object, CancellationToken, TaskCreationOptions) |
使用指定的操作、状态和选项初始化一个新 Task 项。 |
Task(Action)
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
使用指定的操作初始化新 Task 项。
public:
Task(Action ^ action);
public Task(Action action);
new System.Threading.Tasks.Task : Action -> System.Threading.Tasks.Task
Public Sub New (action As Action)
参数
- action
- Action
表示在任务中执行的代码的委托。
例外
参数 action 为 null.
示例
以下示例使用 Task(Action) 构造函数创建在指定目录中检索文件名的任务。 所有任务将文件名写入单个 ConcurrentBag<T> 对象。 然后,该示例调用 WaitAll(Task[]) 该方法以确保所有任务都已完成,然后显示写入对象 ConcurrentBag<T> 的文件名总数的计数。
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
public class Example
{
public static async Task Main()
{
var list = new ConcurrentBag<string>();
string[] dirNames = { ".", ".." };
List<Task> tasks = new List<Task>();
foreach (var dirName in dirNames) {
Task t = new Task( () => { foreach(var path in Directory.GetFiles(dirName))
list.Add(path); } );
tasks.Add(t);
t.Start();
}
await Task.WhenAll(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
open System.Collections.Concurrent
open System.IO
open System.Threading.Tasks
let main =
task {
let list = ConcurrentBag<string>()
let dirNames = [ "."; ".." ]
let tasks = ResizeArray()
for dirName in dirNames do
let t =
new Task(fun () ->
for path in Directory.GetFiles dirName do
list.Add path)
tasks.Add t
t.Start()
do! tasks.ToArray() |> Task.WhenAll
for t in tasks do
printfn $"Task {t.Id} Status: {t.Status}"
printfn $"Number of files read: {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 New Task( Sub()
For Each path In Directory.GetFiles(dirName)
list.Add(path)
Next
End Sub )
tasks.Add(t)
t.Start()
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
下面的示例完全相同,只不过它使用 Run(Action) 该方法在单个操作中实例化和运行任务。 该方法返回 Task 表示任务的对象。
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
open System.Collections.Concurrent
open System.IO
open System.Threading.Tasks
let list = ConcurrentBag<string>()
let dirNames = [ "."; ".." ]
let tasks = ResizeArray()
for dirName in dirNames do
let t =
Task.Run(fun () ->
for path in Directory.GetFiles dirName do
list.Add path)
tasks.Add t
tasks.ToArray() |> Task.WaitAll
for t in tasks do
printfn $"Task {t.Id} Status: {t.Status}"
printfn $"Number of files read: {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
注解
此构造函数仅在需要创建和启动任务分离的高级方案中使用。
与调用此构造函数相比,实例化 Task 对象和启动任务的最常用方法是调用静态 Task.Run(Action) 或 TaskFactory.StartNew(Action) 方法。
如果只需让 API 使用者有待等待的内容,就不需要执行任何操作的任务,则应使用 a TaskCompletionSource<TResult> 。
另请参阅
适用于
Task(Action, CancellationToken)
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
Task使用指定的操作和 CancellationToken.
public:
Task(Action ^ action, System::Threading::CancellationToken cancellationToken);
public Task(Action action, System.Threading.CancellationToken cancellationToken);
new System.Threading.Tasks.Task : Action * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Sub New (action As Action, cancellationToken As CancellationToken)
参数
- action
- Action
表示在任务中执行的代码的委托。
- cancellationToken
- CancellationToken
新 CancellationToken 任务将观察到的。
例外
已释放所提供的 CancellationToken 内容。
参数 action 为 null。
示例
以下示例调用 Task(Action, CancellationToken) 构造函数来创建循环访问 C:\Windows\System32 目录中的文件的任务。 lambda 表达式调用 Parallel.ForEach 该方法,以向对象添加有关每个文件 List<T> 的信息。 循环调用 Parallel.ForEach 的每个分离嵌套任务都会检查取消令牌的状态,如果请求取消,则调用该方法 CancellationToken.ThrowIfCancellationRequested 。 当调用线程调用CancellationToken.ThrowIfCancellationRequested该方法时,该方法OperationCanceledException将catch引发在Task.Wait块中处理的异常。
Start然后调用该方法以启动任务。
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
public class Example
{
public static async Task Main()
{
var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;
var files = new List<Tuple<string, string, long, DateTime>>();
var t = new Task(() => { string dir = "C:\\Windows\\System32\\";
object obj = new Object();
if (Directory.Exists(dir)) {
Parallel.ForEach(Directory.GetFiles(dir),
f => {
if (token.IsCancellationRequested)
token.ThrowIfCancellationRequested();
var fi = new FileInfo(f);
lock(obj) {
files.Add(Tuple.Create(fi.Name, fi.DirectoryName, fi.Length, fi.LastWriteTimeUtc));
}
});
}
} , token);
t.Start();
tokenSource.Cancel();
try {
await t;
Console.WriteLine("Retrieved information for {0} files.", files.Count);
}
catch (AggregateException e) {
Console.WriteLine("Exception messages:");
foreach (var ie in e.InnerExceptions)
Console.WriteLine(" {0}: {1}", ie.GetType().Name, ie.Message);
Console.WriteLine("\nTask status: {0}", t.Status);
}
finally {
tokenSource.Dispose();
}
}
}
// The example displays the following output:
// Exception messages:
// TaskCanceledException: A task was canceled.
//
// Task status: Canceled
open System
open System.IO
open System.Threading
open System.Threading.Tasks
let main =
task {
use tokenSource = new CancellationTokenSource()
let token = tokenSource.Token
let files = ResizeArray()
let t =
new Task(
(fun () ->
let dir = @"C:\Windows\System32\"
let obj = obj ()
if Directory.Exists dir then
Parallel.ForEach(
Directory.GetFiles dir,
fun f ->
if token.IsCancellationRequested then
token.ThrowIfCancellationRequested()
let fi = FileInfo f
lock obj (fun () -> files.Add(fi.Name, fi.DirectoryName, fi.Length, fi.LastWriteTimeUtc))
)
|> ignore),
token
)
t.Start()
tokenSource.Cancel()
try
do! t
printfn $"Retrieved information for {files.Count} files."
with :? AggregateException as e ->
printfn "Exception messages:"
for ie in e.InnerExceptions do
printfn $" {ie.GetType().Name}: {ie.Message}"
printfn $"Task status: {t.Status}"
}
main.Wait()
// The example displays the following output:
// Exception messages:
// TaskCanceledException: A task was canceled.
//
// Task status: Canceled
Imports System.Collections.Generic
Imports System.IO
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim tokenSource As New CancellationTokenSource()
Dim token As CancellationToken = tokenSource.Token
Dim files As New List(Of Tuple(Of String, String, Long, Date))()
Dim t As New Task(Sub()
Dim dir As String = "C:\Windows\System32\"
Dim obj As New Object()
If Directory.Exists(dir)Then
Parallel.ForEach(Directory.GetFiles(dir),
Sub(f)
If token.IsCancellationRequested Then
token.ThrowIfCancellationRequested()
End If
Dim fi As New FileInfo(f)
SyncLock(obj)
files.Add(Tuple.Create(fi.Name, fi.DirectoryName, fi.Length, fi.LastWriteTimeUtc))
End SyncLock
End Sub)
End If
End Sub, token)
t.Start()
tokenSource.Cancel()
Try
t.Wait()
Console.WriteLine("Retrieved information for {0} files.", files.Count)
Catch e As AggregateException
Console.WriteLine("Exception messages:")
For Each ie As Exception In e.InnerExceptions
Console.WriteLine(" {0}:{1}", ie.GetType().Name, ie.Message)
Next
Console.WriteLine()
Console.WriteLine("Task status: {0}", t.Status)
Finally
tokenSource.Dispose()
End Try
End Sub
End Module
' The example displays the following output:
' Exception messages:
' TaskCanceledException: A task was canceled.
'
' Task status: Canceled
注解
与调用此构造函数相比,实例化 Task 对象和启动任务的最常用方法是调用静态 Task.Run(Action, CancellationToken) 和 TaskFactory.StartNew(Action, CancellationToken) 方法。 此构造函数提供的唯一优势是它允许对象实例化与任务调用分离。
有关详细信息,请参阅托管线程中的任务并行度(任务并行库)和取消。
适用于
Task(Action, TaskCreationOptions)
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
使用指定的操作和创建选项初始化一个新 Task 项。
public:
Task(Action ^ action, System::Threading::Tasks::TaskCreationOptions creationOptions);
public Task(Action action, System.Threading.Tasks.TaskCreationOptions creationOptions);
new System.Threading.Tasks.Task : Action * System.Threading.Tasks.TaskCreationOptions -> System.Threading.Tasks.Task
Public Sub New (action As Action, creationOptions As TaskCreationOptions)
参数
- action
- Action
表示在任务中执行的代码的委托。
- creationOptions
- TaskCreationOptions
TaskCreationOptions用于自定义任务的行为。
例外
参数 action 为 null。
该 creationOptions 参数指定了无效 TaskCreationOptions值。
注解
实例化 Task 对象和启动任务的最常用方法是调用静态 TaskFactory.StartNew(Action, TaskCreationOptions) 方法,而不是调用此构造函数。 此构造函数提供的唯一优势是它允许对象实例化与任务调用分离。
适用于
Task(Action<Object>, Object)
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
使用指定的操作和状态初始化新 Task 项。
public:
Task(Action<System::Object ^> ^ action, System::Object ^ state);
public Task(Action<object> action, object state);
public Task(Action<object?> action, object? state);
new System.Threading.Tasks.Task : Action<obj> * obj -> System.Threading.Tasks.Task
Public Sub New (action As Action(Of Object), state As Object)
参数
- state
- Object
一个对象,表示操作要使用的数据。
例外
参数 action 为 null。
示例
以下示例定义 6 个字母单词的数组。 然后,每个单词作为参数 Task(Action<Object>, Object) 传递给构造函数,该构造函数的 Action<T> 委托会争先恐后地处理单词中的字符,然后显示原始单词及其争用版本。
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
public class Example
{
public static async Task Main()
{
var tasks = new List<Task>();
Random rnd = new Random();
Object lockObj = new Object();
String[] words6 = { "reason", "editor", "rioter", "rental",
"senior", "regain", "ordain", "rained" };
foreach (var word6 in words6) {
Task t = new Task( (word) => { Char[] chars = word.ToString().ToCharArray();
double[] order = new double[chars.Length];
lock (lockObj) {
for (int ctr = 0; ctr < order.Length; ctr++)
order[ctr] = rnd.NextDouble();
}
Array.Sort(order, chars);
Console.WriteLine("{0} --> {1}", word,
new String(chars));
}, word6);
t.Start();
tasks.Add(t);
}
await Task.WhenAll(tasks.ToArray());
}
}
// The example displays output like the following:
// regain --> irnaeg
// ordain --> rioadn
// reason --> soearn
// rained --> rinade
// rioter --> itrore
// senior --> norise
// rental --> atnerl
// editor --> oteird
open System
open System.Threading.Tasks
let main =
task {
let tasks = ResizeArray()
let rnd = Random()
let lockObj = obj ()
let words6 =
[ "reason"
"editor"
"rioter"
"rental"
"senior"
"regain"
"ordain"
"rained" ]
for word6 in words6 do
let t =
new Task(
(fun word ->
let chars = (string word).ToCharArray()
let order = Array.zeroCreate<double> chars.Length
lock lockObj (fun () ->
for i = 0 to order.Length - 1 do
order[i] <- rnd.NextDouble())
Array.Sort(order, chars)
printfn $"{word} --> {new String(chars)}"),
word6
)
t.Start()
tasks.Add t
do! tasks.ToArray() |> Task.WhenAll
}
main.Wait()
// The example displays output like the following:
// regain --> irnaeg
// ordain --> rioadn
// reason --> soearn
// rained --> rinade
// rioter --> itrore
// senior --> norise
// rental --> atnerl
// editor --> oteird
Imports System.Collections.Generic
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim tasks As New List(Of Task)()
Dim rnd As New Random()
Dim lockObj As New Object()
Dim words6() As String = { "reason", "editor", "rioter", "rental",
"senior", "regain", "ordain", "rained" }
For Each word6 in words6
Dim t As New Task( Sub(word)
Dim chars() As Char = word.ToString().ToCharArray()
Dim order(chars.Length - 1) As Double
SyncLock lockObj
For ctr As Integer = 0 To order.Length - 1
order(ctr) = rnd.NextDouble()
Next
End SyncLock
Array.Sort(order, chars)
Console.WriteLine("{0} --> {1}", word,
New String(chars))
End Sub, word6)
t.Start()
tasks.Add(t)
Next
Task.WaitAll(tasks.ToArray())
End Sub
End Module
' The example displays output like the following:
' regain --> irnaeg
' ordain --> rioadn
' reason --> soearn
' rained --> rinade
' rioter --> itrore
' senior --> norise
' rental --> atnerl
' editor --> oteird
注解
实例化 Task 对象和启动任务的最常用方法是调用静态 TaskFactory.StartNew(Action<Object>, Object) 方法,而不是调用此构造函数。 此构造函数提供的唯一优势是它允许对象实例化与任务调用分离。
另请参阅
适用于
Task(Action, CancellationToken, TaskCreationOptions)
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
使用指定的操作和创建选项初始化一个新 Task 项。
public:
Task(Action ^ action, System::Threading::CancellationToken cancellationToken, System::Threading::Tasks::TaskCreationOptions creationOptions);
public Task(Action action, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskCreationOptions creationOptions);
new System.Threading.Tasks.Task : Action * System.Threading.CancellationToken * System.Threading.Tasks.TaskCreationOptions -> System.Threading.Tasks.Task
Public Sub New (action As Action, cancellationToken As CancellationToken, creationOptions As TaskCreationOptions)
参数
- action
- Action
表示在任务中执行的代码的委托。
- cancellationToken
- CancellationToken
新 CancellationToken 任务将观察到的。
- creationOptions
- TaskCreationOptions
TaskCreationOptions用于自定义任务的行为。
例外
CancellationTokenSource已释放创建的cancellationToken项。
参数 action 为 null。
该 creationOptions 参数指定了无效 TaskCreationOptions值。
注解
实例化 Task 对象和启动任务的最常用方法是调用静态 TaskFactory.StartNew(Action, CancellationToken, TaskCreationOptions, TaskScheduler) 方法,而不是调用此构造函数。 此构造函数提供的唯一优势是它允许对象实例化与任务调用分离。
有关详细信息,请参阅 任务并行度(任务并行库) 和 任务取消。
适用于
Task(Action<Object>, Object, CancellationToken)
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
Task使用指定的操作、状态和 CancellationToken.
public:
Task(Action<System::Object ^> ^ action, System::Object ^ state, System::Threading::CancellationToken cancellationToken);
public Task(Action<object> action, object state, System.Threading.CancellationToken cancellationToken);
public Task(Action<object?> action, object? state, System.Threading.CancellationToken cancellationToken);
new System.Threading.Tasks.Task : Action<obj> * obj * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Sub New (action As Action(Of Object), state As Object, cancellationToken As CancellationToken)
参数
- state
- Object
一个对象,表示操作要使用的数据。
- cancellationToken
- CancellationToken
新 CancellationToken 任务将观察到的。
例外
CancellationTokenSource已释放创建的cancellationToken项。
参数 action 为 null。
注解
实例化 Task 对象和启动任务的最常用方法是调用静态 TaskFactory.StartNew(Action<Object>, Object, CancellationToken) 方法,而不是调用此构造函数。 此构造函数提供的唯一优势是它允许对象实例化与任务调用分离。
适用于
Task(Action<Object>, Object, TaskCreationOptions)
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
使用指定的操作、状态和选项初始化一个新 Task 项。
public:
Task(Action<System::Object ^> ^ action, System::Object ^ state, System::Threading::Tasks::TaskCreationOptions creationOptions);
public Task(Action<object> action, object state, System.Threading.Tasks.TaskCreationOptions creationOptions);
public Task(Action<object?> action, object? state, System.Threading.Tasks.TaskCreationOptions creationOptions);
new System.Threading.Tasks.Task : Action<obj> * obj * System.Threading.Tasks.TaskCreationOptions -> System.Threading.Tasks.Task
Public Sub New (action As Action(Of Object), state As Object, creationOptions As TaskCreationOptions)
参数
- state
- Object
一个对象,表示操作要使用的数据。
- creationOptions
- TaskCreationOptions
TaskCreationOptions用于自定义任务的行为。
例外
参数 action 为 null。
该 creationOptions 参数指定了无效 TaskCreationOptions值。
注解
实例化 Task 对象和启动任务的最常用方法是调用静态 TaskFactory.StartNew(Action<Object>, Object, TaskCreationOptions) 方法,而不是调用此构造函数。 此构造函数提供的唯一优势是它允许对象实例化与任务调用分离。
适用于
Task(Action<Object>, Object, CancellationToken, TaskCreationOptions)
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
- Source:
- Task.cs
使用指定的操作、状态和选项初始化一个新 Task 项。
public:
Task(Action<System::Object ^> ^ action, System::Object ^ state, System::Threading::CancellationToken cancellationToken, System::Threading::Tasks::TaskCreationOptions creationOptions);
public Task(Action<object> action, object state, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskCreationOptions creationOptions);
public Task(Action<object?> action, object? state, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskCreationOptions creationOptions);
new System.Threading.Tasks.Task : Action<obj> * obj * System.Threading.CancellationToken * System.Threading.Tasks.TaskCreationOptions -> System.Threading.Tasks.Task
Public Sub New (action As Action(Of Object), state As Object, cancellationToken As CancellationToken, creationOptions As TaskCreationOptions)
参数
- state
- Object
一个对象,表示操作要使用的数据。
- cancellationToken
- CancellationToken
新 CancellationToken 任务将观察到的。
- creationOptions
- TaskCreationOptions
TaskCreationOptions用于自定义任务的行为。
例外
CancellationTokenSource已释放创建的cancellationToken项。
参数 action 为 null。
该 creationOptions 参数指定了无效 TaskCreationOptions值。
注解
实例化 Task 对象和启动任务的最常用方法是调用静态 TaskFactory.StartNew(Action<Object>, Object, CancellationToken, TaskCreationOptions, TaskScheduler) 方法,而不是调用此构造函数。 此构造函数提供的唯一优势是它允许对象实例化与任务调用分离。