本文整理匯總了VB.NET中System.Threading.Tasks.Task.Run方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Task.Run方法的具體用法?VB.NET Task.Run怎麽用?VB.NET Task.Run使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Threading.Tasks.Task
的用法示例。
在下文中一共展示了Task.Run方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Example
' 導入命名空間
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
輸出:
Application thread ID: 1 Task thread ID: 3
示例2: Example
' 導入命名空間
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
輸出:
Application thread ID: 1 Task thread ID: 3
示例3: Example
' 導入命名空間
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
輸出:
Task 1 Status: RanToCompletion Task 2 Status: RanToCompletion Number of files read: 23
示例4: Example
' 導入命名空間
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 Task = Task.Run( 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)
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
輸出:
Exception messages: TaskCanceledException: A task was canceled. Task status: Canceled
示例5: Example
' 導入命名空間
Imports System.Collections.Generic
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim tasks As New List(Of Task(Of Integer))()
Dim source As New CancellationTokenSource
Dim token As CancellationToken = source.Token
Dim completedIterations As Integer = 0
For n As Integer = 0 To 19
tasks.Add(Task.Run( Function()
Dim iterations As Integer= 0
For ctr As Long = 1 To 2000000
token.ThrowIfCancellationRequested()
iterations += 1
Next
Interlocked.Increment(completedIterations)
If completedIterations >= 10 Then source.Cancel()
Return iterations
End Function, token))
Next
Console.WriteLine("Waiting for the first 10 tasks to complete... ")
Try
Task.WaitAll(tasks.ToArray())
Catch e As AggregateException
Console.WriteLine("Status of tasks:")
Console.WriteLine()
Console.WriteLine("{0,10} {1,20} {2,14}", "Task Id",
"Status", "Iterations")
For Each t In tasks
Console.WriteLine("{0,10} {1,20} {2,14}",
t.Id, t.Status,
If(t.Status <> TaskStatus.Canceled,
t.Result.ToString("N0"), "n/a"))
Next
End Try
End Sub
End Module
輸出:
Waiting for the first 10 tasks to complete... Status of tasks: Task Id Status Iterations 1 RanToCompletion 2,000,000 2 RanToCompletion 2,000,000 3 RanToCompletion 2,000,000 4 RanToCompletion 2,000,000 5 RanToCompletion 2,000,000 6 RanToCompletion 2,000,000 7 RanToCompletion 2,000,000 8 RanToCompletion 2,000,000 9 RanToCompletion 2,000,000 10 Canceled n/a 11 Canceled n/a 12 Canceled n/a 13 Canceled n/a 14 Canceled n/a 15 Canceled n/a 16 RanToCompletion 2,000,000 17 Canceled n/a 18 Canceled n/a 19 Canceled n/a 20 Canceled n/a
示例6: Example
' 導入命名空間
Imports System.Collections.Generic
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim tasks As New List(Of Task(Of Integer))()
Dim source As New CancellationTokenSource
Dim token As CancellationToken = source.Token
Dim completedIterations As Integer = 0
For n As Integer = 0 To 19
tasks.Add(Task.Run( Function()
Dim iterations As Integer= 0
For ctr As Long = 1 To 2000000
If token.IsCancellationRequested Then
Return iterations
End If
iterations += 1
Next
Interlocked.Increment(completedIterations)
If completedIterations >= 10 Then source.Cancel()
Return iterations
End Function, token))
Next
Try
Task.WaitAll(tasks.ToArray())
Catch e As AggregateException
Console.WriteLine("Status of tasks:")
Console.WriteLine()
Console.WriteLine("{0,10} {1,20} {2,14:N0}", "Task Id",
"Status", "Iterations")
For Each t In tasks
Console.WriteLine("{0,10} {1,20} {2,14}",
t.Id, t.Status,
If(t.Status <> TaskStatus.Canceled,
t.Result.ToString("N0"), "Not Started"))
Next
End Try
End Sub
End Module
輸出:
Status of tasks: Task Id Status Iterations 1 RanToCompletion 2,000,000 2 RanToCompletion 2,000,000 3 RanToCompletion 2,000,000 4 RanToCompletion 2,000,000 5 RanToCompletion 2,000,000 6 RanToCompletion 2,000,000 7 RanToCompletion 2,000,000 8 RanToCompletion 2,000,000 9 RanToCompletion 2,000,000 10 RanToCompletion 1,658,326 11 RanToCompletion 1,988,506 12 RanToCompletion 2,000,000 13 RanToCompletion 1,942,246 14 RanToCompletion 950,108 15 RanToCompletion 1,837,832 16 RanToCompletion 1,687,182 17 RanToCompletion 194,548 18 Canceled Not Started 19 Canceled Not Started 20 Canceled Not Started
示例7: Example
' 導入命名空間
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim pattern As String = "\p{P}*\s+"
Dim titles() As String = { "Sister Carrie",
"The Financier" }
Dim tasks(titles.Length - 1) As Task(Of Integer)
For ctr As Integer = 0 To titles.Length - 1
Dim s As String = titles(ctr)
tasks(ctr) = Task.Run( Function()
' Number of words.
Dim nWords As Integer = 0
' Create filename from title.
Dim fn As String = s + ".txt"
If File.Exists(fn) Then
Dim sr As New StreamReader(fn)
Dim input As String = sr.ReadToEndAsync().Result
nWords = Regex.Matches(input, pattern).Count
End If
Return nWords
End Function)
Next
Task.WaitAll(tasks)
Console.WriteLine("Word Counts:")
Console.WriteLine()
For ctr As Integer = 0 To titles.Length - 1
Console.WriteLine("{0}: {1,10:N0} words", titles(ctr), tasks(ctr).Result)
Next
End Sub
End Module
輸出:
Sister Carrie: 159,374 words The Financier: 196,362 words