• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: 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 New Task( Sub()
                               For Each path In Directory.GetFiles(dirName)
                                  list.Add(path)
                               Next
                            End Sub  )
         tasks.Add(t)
         t.Start()
      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
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:27,代码来源:Task

输出:

Task 1 Status: RanToCompletion
Task 2 Status: RanToCompletion
Number of files read: 23


示例2: 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
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:26,代码来源:Task

输出:

Task 1 Status: RanToCompletion
Task 2 Status: RanToCompletion
Number of files read: 23


示例3: 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 New Task(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)
      t.Start()
      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
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:44,代码来源:Task

输出:

Exception messages:
TaskCanceledException: A task was canceled.

Task status: Canceled


示例4: Example

' 导入命名空间
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()
                                  Next
                               End SyncLock
                               Array.Sort(order, chars)
                               Console.WriteLine("{0} --> {1}", word,
                                                 New String(chars))
                            End Sub, word6)
         t.Start()
         tasks.Add(t)
      Next
      Task.WaitAll(tasks.ToArray())
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:31,代码来源:Task

输出:

regain --> irnaeg
ordain --> rioadn
reason --> soearn
rained --> rinade
rioter --> itrore
senior --> norise
rental --> atnerl
editor --> oteird



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
VB.NET PageAsyncTask构造函数代码示例发布时间:2022-05-24
下一篇:
VB.NET UnicodeEncoding构造函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap