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


VB.NET Parallel.ForEach方法代码示例

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


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

示例1: ForEachDemo

' 导入命名空间
Imports System.Threading
Imports System.Threading.Tasks

Module ForEachDemo

    ' Demonstrated features:
    '   Parallel.ForEach()
    '   Thread-local state
    ' Expected results:
    '   This example sums up the elements of an int[] in parallel.
    '   Each thread maintains a local sum. When a thread is initialized, that local sum is set to 0.
    '   On every iteration the current element is added to the local sum.
    '   When a thread is done, it safely adds its local sum to the global sum.
    '   After the loop is complete, the global sum is printed out.
    ' Documentation:
    '   http://msdn.microsoft.com/library/dd990270(VS.100).aspx
    Private Sub ForEachDemo()
        ' The sum of these elements is 40.
        Dim input As Integer() = {4, 1, 6, 2, 9, 5, _
        10, 3}
        Dim sum As Integer = 0

        Try
            ' source collection
            Parallel.ForEach(input,
                             Function()
                                 ' thread local initializer
                                 Return 0
                             End Function,
                             Function(n, loopState, localSum)
                                 ' body
                                 localSum += n
                                 Console.WriteLine("Thread={0}, n={1}, localSum={2}", Thread.CurrentThread.ManagedThreadId, n, localSum)
                                 Return localSum
                             End Function,
                             Sub(localSum)
                                 ' thread local aggregator
                                 Interlocked.Add(sum, localSum)
                             End Sub)

            Console.WriteLine(vbLf & "Sum={0}", sum)
        Catch e As AggregateException
            ' No exception is expected in this example, but if one is still thrown from a task,
            ' it will be wrapped in AggregateException and propagated to the main thread.
            Console.WriteLine("Parallel.ForEach has thrown an exception. THIS WAS NOT EXPECTED." & vbLf & "{0}", e)
        End Try
    End Sub


End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:51,代码来源:Parallel.ForEach

示例2: Example

' 导入命名空间
Imports System.IO
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim task As Task(Of String) = ReadCharacters(".\CallOfTheWild.txt")
      Dim text As String = task.Result
      
      Dim nVowels As Integer = 0
      Dim nNonWhiteSpace As Integer = 0
      Dim obj As New Object()

      Dim result As ParallelLoopResult = Parallel.ForEach(text, 
                                                          Sub(ch)
                                                             Dim uCh As Char = Char.ToUpper(ch)
                                                             If "AEIOUY".IndexOf(uCh) >= 0 Then
                                                                SyncLock obj
                                                                   nVowels += 1
                                                                End SyncLock
                                                             End If
                                                             If Not Char.IsWhiteSpace(uCh) Then
                                                                SyncLock obj
                                                                   nNonWhiteSpace += 1
                                                                End SyncLock   
                                                             End If
                                                          End Sub)
      Console.WriteLine("Total characters:      {0,10:N0}", text.Length)
      Console.WriteLine("Total vowels:          {0,10:N0}", nVowels)
      Console.WriteLine("Total non-white space:  {0,10:N0}", nNonWhiteSpace)
   End Sub
   
   Private Async Function ReadCharacters(fn As String) As Task(Of String)
      Dim text As String
      Using sr As New StreamReader(fn)
         text = Await sr.ReadToEndAsync()
      End Using
      Return text
   End Function
End Module
' The output from the example resembles the following:
'       Total characters:         198,548
'       Total vowels:              58,421
'       Total non-white space:     159,461
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:44,代码来源:Parallel.ForEach

示例3: RangePartitionerDemo

' 导入命名空间
Imports System.Collections.Concurrent
Imports System.Threading
Imports System.Threading.Tasks

Module RangePartitionerDemo

    Sub Main()
        Dim sw As Stopwatch = Nothing

        Dim sum As Long = 0
        Dim SUMTOP As Long = 10000000

        ' Try sequential for
        sw = Stopwatch.StartNew()
        For i As Long = 0 To SUMTOP - 1
            sum += i
        Next
        sw.Stop()
        Console.WriteLine("sequential for result = {0}, time = {1} ms", sum, sw.ElapsedMilliseconds)

        ' Try parallel for with locals
        sum = 0
        sw = Stopwatch.StartNew()
        Parallel.For(0L, SUMTOP, Function() 0L, Function(item, state, prevLocal) prevLocal + item, Function(local) Interlocked.Add(sum, local))
        sw.Stop()
        Console.WriteLine("parallel for w/locals result = {0}, time = {1} ms", sum, sw.ElapsedMilliseconds)

        ' Try range partitioner
        sum = 0
        sw = Stopwatch.StartNew()
        Parallel.ForEach(Partitioner.Create(0L, SUMTOP),
                         Sub(range)
                             Dim local As Long = 0
                             For i As Long = range.Item1 To range.Item2 - 1
                                 local += i
                             Next
                             Interlocked.Add(sum, local)
                         End Sub)
        sw.Stop()
        Console.WriteLine("range partitioner result = {0}, time = {1} ms", sum, sw.ElapsedMilliseconds)
    End Sub

End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:44,代码来源:Parallel.ForEach


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