当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET TaskFactory.ContinueWhenAll方法代码示例

本文整理汇总了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
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:71,代码来源:TaskFactory.ContinueWhenAll

输出:

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
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:61,代码来源:TaskFactory.ContinueWhenAll

输出:

chapter2.txt               1,585 words
chapter1.txt               4,012 words

Failure to determine total word count: a task was cancelled.


注:本文中的System.Threading.Tasks.TaskFactory.ContinueWhenAll方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。