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()
End SyncLock
Array.Sort(order, chars)
Console.WriteLine("{0} --> {1}", word,
New String(chars))
End Sub, word6)
t.Start()
tasks.Add(t)
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)
方法。 這個建構函式所提供的唯一優點是它允許物件具現化與工作調用分開。
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)
若要具現化
Task
物件並啟動工作,而不是呼叫這個建構函式,最常見的方式是呼叫靜態
TaskFactory.StartNew(Action, CancellationToken, TaskCreationOptions, TaskScheduler)
方法。 這個建構函式所提供的唯一優點是它允許物件具現化與工作調用分開。
如需詳細資訊,請參閱
工作平行處理原則 (工作平行程式庫)
和
工作取消
。
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)
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)
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)