本文整理匯總了VB.NET中System.Threading.Tasks.TaskFactory.ContinueWhenAll方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET TaskFactory.ContinueWhenAll方法的具體用法?VB.NET TaskFactory.ContinueWhenAll怎麽用?VB.NET TaskFactory.ContinueWhenAll使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Threading.Tasks.TaskFactory
的用法示例。
在下文中一共展示了TaskFactory.ContinueWhenAll方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Example
' 導入命名空間
Imports System.Collections.Generic
Imports System.IO
Imports System.Threading
Imports System.Threading.Tasks
Imports System.Text.RegularExpressions
Module Example
Dim totalWords As Integer = 0
Public Sub Main()
Dim filenames() As String = { "chapter1.txt", "chapter2.txt",
"chapter3.txt", "chapter4.txt",
"chapter5.txt" }
Dim pattern As String = "\b\w+\b"
Dim tasks As New List(Of Task)()
' Determine the number of words in each file.
For Each filename In filenames
tasks.Add(Task.Factory.StartNew( Sub(fn)
If Not File.Exists(filename)
Throw New FileNotFoundException("{0} does not exist.", filename)
End If
Dim sr As New StreamReader(fn.ToString())
Dim content As String = sr.ReadToEnd()
sr.Close()
Dim words As Integer = Regex.Matches(content, pattern).Count
Interlocked.Add(totalWords, words)
Console.WriteLine("{0,-25} {1,6:N0} words", fn, words)
End Sub, filename))
Next
Dim finalTask As Task = Task.Factory.ContinueWhenAll(tasks.ToArray(),
Sub(wordCountTasks As Task() )
Dim nSuccessfulTasks As Integer = 0
Dim nFailed As Integer = 0
Dim nFileNotFound As Integer = 0
For Each t In wordCountTasks
If t.Status = TaskStatus.RanToCompletion Then _
nSuccessfulTasks += 1
If t.Status = TaskStatus.Faulted Then
nFailed += 1
t.Exception.Handle(Function(e As Exception)
If TypeOf e Is FileNotFoundException Then
nFileNotFound += 1
End If
Return True
End Function)
End If
Next
Console.WriteLine()
Console.WriteLine("{0,-25} {1,6} total words",
String.Format("{0} files", nSuccessfulTasks),
totalWords)
If nFailed > 0 Then
Console.WriteLine()
Console.WriteLine("{0} tasks failed for the following reasons:", nFailed)
Console.WriteLine(" File not found: {0}", nFileNotFound)
If nFailed <> nFileNotFound Then
Console.WriteLine(" Other: {0}", nFailed - nFileNotFound)
End If
End If
End Sub)
finalTask.Wait()
End Sub
Private Sub DisplayResult(wordCountTasks As Task())
End Sub
End Module
輸出:
chapter2.txt 1,585 words chapter1.txt 4,012 words chapter5.txt 4,660 words chapter3.txt 7,481 words 4 files 17738 total words 1 tasks failed for the following reasons: File not found: 1
示例2: Example
' 導入命名空間
Imports System.Collections.Generic
Imports System.IO
Imports System.Threading
Imports System.Threading.Tasks
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim filenames() As String= { "chapter1.txt", "chapter2.txt",
"chapter3.txt", "chapter4.txt",
"chapter5.txt" }
Dim pattern As String = "\b\w+\b"
Dim tasks As New List(Of Task)()
Dim source As New CancellationTokenSource()
Dim token As CancellationToken = source.Token
Dim totalWords As Integer = 0
' Determine the number of words in each file.
For Each filename In filenames
tasks.Add( Task.Factory.StartNew( Sub(obj As Object)
Dim fn As String = CStr(obj)
token.ThrowIfCancellationRequested()
If Not File.Exists(fn) Then
source.Cancel()
token.ThrowIfCancellationRequested()
End If
Dim sr As New StreamReader(fn.ToString())
Dim content As String = sr.ReadToEnd()
sr.Close()
Dim words As Integer = Regex.Matches(content, pattern).Count
Interlocked.Add(totalWords, words)
Console.WriteLine("{0,-25} {1,6:N0} words", fn, words)
End Sub, filename, token))
Next
Dim finalTask As Task = Task.Factory.ContinueWhenAll(tasks.ToArray(),
Sub(wordCountTasks As Task())
If Not token.IsCancellationRequested Then
Console.WriteLine("\n{0,-25} {1,6} total words\n",
String.Format("{0} files", wordCountTasks.Length),
totalWords)
End If
End Sub, token)
Try
finalTask.Wait()
Catch ae As AggregateException
For Each inner In ae.InnerExceptions
Console.WriteLine()
If TypeOf inner Is TaskCanceledException
Console.WriteLine("Failure to determine total word count: a task was cancelled.")
Else
Console.WriteLine("Failure caused by {0}", inner.GetType().Name)
End If
Next
Finally
source.Dispose()
End Try
End Sub
End Module
輸出:
chapter2.txt 1,585 words chapter1.txt 4,012 words Failure to determine total word count: a task was cancelled.