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


VB.NET Task<TResult>构造函数代码示例

本文整理汇总了VB.NET中System.Threading.Tasks.Task<TResult>.Task构造函数的典型用法代码示例。如果您正苦于以下问题:VB.NET Task<TResult>构造函数的具体用法?VB.NET Task<TResult>怎么用?VB.NET Task<TResult>使用的例子?那么恭喜您, 这里精选的构造函数代码示例或许可以为您提供帮助。您也可以进一步了解该构造函数所在System.Threading.Tasks.Task<TResult>的用法示例。


在下文中一共展示了Task<TResult>构造函数的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: 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) = New Task(Of Integer)( Function()
                                   ' Number of words.
                                   Dim nWords As Integer = 0
                                   ' Create filename from title.
                                   Dim fn As String = s + ".txt"

                                   Dim sr As New StreamReader(fn)
                                   Dim input As String = sr.ReadToEndAsync().Result
                                   sr.Close()
                                   nWords = Regex.Matches(input, pattern).Count
                                   Return nWords
                                End Function)
      Next

      ' Ensure files exist before launching tasks.
      Dim allExist As Boolean = True
      For Each title In titles
         Dim fn As String = title + ".txt"
         If Not File.Exists(fn) Then
            allExist = false
            Console.WriteLine("Cannot find '{0}'", fn)
            Exit For
         End If   
      Next
      ' Launch tasks 
      If allExist Then
         For Each t in tasks
            t.Start()
         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 If   
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:53,代码来源:Task

输出:

Sister Carrie:    159,374 words
The Financier:    196,362 words


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