ParallelLoopState 类

定义

可使并行循环迭代与其他迭代交互。 此类的实例由 Parallel 类提供给每个循环;不能在你的用户代码中创建实例。

public ref class ParallelLoopState
public class ParallelLoopState
type ParallelLoopState = class
Public Class ParallelLoopState
继承
ParallelLoopState

示例

以下示例并行执行循环的最多 100 次迭代。 每次迭代暂停 1 到 1,000 毫秒的随机间隔。 随机生成的值确定调用方法的循环 Break 迭代。 如示例输出所示,在调用 Break 方法后,没有索引大于LowestBreakIteration属性值的迭代开始。

using System;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
    public static void Main()
    {
        var rnd = new Random();
        int breakIndex = rnd.Next(1, 11);

        Console.WriteLine($"Will call Break at iteration {breakIndex}\n");

        var result = Parallel.For(1, 101, (i, state) => 
        {
            Console.WriteLine($"Beginning iteration {i}");
            int delay;
            lock (rnd)
                delay = rnd.Next(1, 1001);
            Thread.Sleep(delay);

            if (state.ShouldExitCurrentIteration)
            {
                if (state.LowestBreakIteration < i)
                    return;
            }

            if (i == breakIndex)
            {
                Console.WriteLine($"Break in iteration {i}");
                state.Break();
            }

            Console.WriteLine($"Completed iteration {i}");
        });

        if (result.LowestBreakIteration.HasValue)
            Console.WriteLine($"\nLowest Break Iteration: {result.LowestBreakIteration}");
        else
            Console.WriteLine($"\nNo lowest break iteration.");
    }
}
// The example displays output like the following:
//       Will call Break at iteration 8
//
//       Beginning iteration 1
//       Beginning iteration 13
//       Beginning iteration 97
//       Beginning iteration 25
//       Beginning iteration 49
//       Beginning iteration 37
//       Beginning iteration 85
//       Beginning iteration 73
//       Beginning iteration 61
//       Completed iteration 85
//       Beginning iteration 86
//       Completed iteration 61
//       Beginning iteration 62
//       Completed iteration 86
//       Beginning iteration 87
//       Completed iteration 37
//       Beginning iteration 38
//       Completed iteration 38
//       Beginning iteration 39
//       Completed iteration 25
//       Beginning iteration 26
//       Completed iteration 26
//       Beginning iteration 27
//       Completed iteration 73
//       Beginning iteration 74
//       Completed iteration 62
//       Beginning iteration 63
//       Completed iteration 39
//       Beginning iteration 40
//       Completed iteration 40
//       Beginning iteration 41
//       Completed iteration 13
//       Beginning iteration 14
//       Completed iteration 1
//       Beginning iteration 2
//       Completed iteration 97
//       Beginning iteration 98
//       Completed iteration 49
//       Beginning iteration 50
//       Completed iteration 87
//       Completed iteration 27
//       Beginning iteration 28
//       Completed iteration 50
//       Beginning iteration 51
//       Beginning iteration 88
//       Completed iteration 14
//       Beginning iteration 15
//       Completed iteration 15
//       Completed iteration 2
//       Beginning iteration 3
//       Beginning iteration 16
//       Completed iteration 63
//       Beginning iteration 64
//       Completed iteration 74
//       Beginning iteration 75
//       Completed iteration 41
//       Beginning iteration 42
//       Completed iteration 28
//       Beginning iteration 29
//       Completed iteration 29
//       Beginning iteration 30
//       Completed iteration 98
//       Beginning iteration 99
//       Completed iteration 64
//       Beginning iteration 65
//       Completed iteration 42
//       Beginning iteration 43
//       Completed iteration 88
//       Beginning iteration 89
//       Completed iteration 51
//       Beginning iteration 52
//       Completed iteration 16
//       Beginning iteration 17
//       Completed iteration 43
//       Beginning iteration 44
//       Completed iteration 44
//       Beginning iteration 45
//       Completed iteration 99
//       Beginning iteration 4
//       Completed iteration 3
//       Beginning iteration 8
//       Completed iteration 4
//       Beginning iteration 5
//       Completed iteration 52
//       Beginning iteration 53
//       Completed iteration 75
//       Beginning iteration 76
//       Completed iteration 76
//       Beginning iteration 77
//       Completed iteration 65
//       Beginning iteration 66
//       Completed iteration 5
//       Beginning iteration 6
//       Completed iteration 89
//       Beginning iteration 90
//       Completed iteration 30
//       Beginning iteration 31
//       Break in iteration 8
//       Completed iteration 8
//       Completed iteration 6
//       Beginning iteration 7
//       Completed iteration 7
//
//       Lowest Break Iteration: 8
Imports System.Threading
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim rnd As New Random()
      Dim breakIndex As Integer = rnd.Next(1, 11)
      Dim lowest As New Nullable(Of Long)()

      Console.WriteLine("Will call Break at iteration {0}", breakIndex)
      Console.WriteLine()

      Dim result = Parallel.For(1, 101, Sub(i, state)
                                            Console.WriteLine("Beginning iteration {0}", i)
                                            Dim delay As Integer
                                            Monitor.Enter(rnd)
                                               delay = rnd.Next(1, 1001)
                                            Monitor.Exit(rnd)
                                            Thread.Sleep(delay)

                                            If state.ShouldExitCurrentIteration Then
                                               If state.LowestBreakIteration < i Then
                                                  Return
                                               End If
                                            End If

                                            If i = breakIndex Then
                                               Console.WriteLine("Break in iteration {0}", i)
                                               state.Break()
                                               If state.LowestBreakIteration.HasValue Then
                                                  If lowest < state.LowestBreakIteration Then
                                                     lowest = state.LowestBreakIteration
                                                  Else
                                                     lowest = state.LowestBreakIteration
                                                  End If
                                               End If
                                            End If

                                            Console.WriteLine("Completed iteration {0}", i)
                                       End Sub )
         Console.WriteLine()
         If lowest.HasValue Then
            Console.WriteLine("Lowest Break Iteration: {0}", lowest)
         Else
            Console.WriteLine("No lowest break iteration.")
         End If
   End Sub
End Module
' The example displays output like the following:
'       Will call Break at iteration 8
'
'       Beginning iteration 1
'       Beginning iteration 13
'       Beginning iteration 97
'       Beginning iteration 25
'       Beginning iteration 49
'       Beginning iteration 37
'       Beginning iteration 85
'       Beginning iteration 73
'       Beginning iteration 61
'       Completed iteration 85
'       Beginning iteration 86
'       Completed iteration 61
'       Beginning iteration 62
'       Completed iteration 86
'       Beginning iteration 87
'       Completed iteration 37
'       Beginning iteration 38
'       Completed iteration 38
'       Beginning iteration 39
'       Completed iteration 25
'       Beginning iteration 26
'       Completed iteration 26
'       Beginning iteration 27
'       Completed iteration 73
'       Beginning iteration 74
'       Completed iteration 62
'       Beginning iteration 63
'       Completed iteration 39
'       Beginning iteration 40
'       Completed iteration 40
'       Beginning iteration 41
'       Completed iteration 13
'       Beginning iteration 14
'       Completed iteration 1
'       Beginning iteration 2
'       Completed iteration 97
'       Beginning iteration 98
'       Completed iteration 49
'       Beginning iteration 50
'       Completed iteration 87
'       Completed iteration 27
'       Beginning iteration 28
'       Completed iteration 50
'       Beginning iteration 51
'       Beginning iteration 88
'       Completed iteration 14
'       Beginning iteration 15
'       Completed iteration 15
'       Completed iteration 2
'       Beginning iteration 3
'       Beginning iteration 16
'       Completed iteration 63
'       Beginning iteration 64
'       Completed iteration 74
'       Beginning iteration 75
'       Completed iteration 41
'       Beginning iteration 42
'       Completed iteration 28
'       Beginning iteration 29
'       Completed iteration 29
'       Beginning iteration 30
'       Completed iteration 98
'       Beginning iteration 99
'       Completed iteration 64
'       Beginning iteration 65
'       Completed iteration 42
'       Beginning iteration 43
'       Completed iteration 88
'       Beginning iteration 89
'       Completed iteration 51
'       Beginning iteration 52
'       Completed iteration 16
'       Beginning iteration 17
'       Completed iteration 43
'       Beginning iteration 44
'       Completed iteration 44
'       Beginning iteration 45
'       Completed iteration 99
'       Beginning iteration 4
'       Completed iteration 3
'       Beginning iteration 8
'       Completed iteration 4
'       Beginning iteration 5
'       Completed iteration 52
'       Beginning iteration 53
'       Completed iteration 75
'       Beginning iteration 76
'       Completed iteration 76
'       Beginning iteration 77
'       Completed iteration 65
'       Beginning iteration 66
'       Completed iteration 5
'       Beginning iteration 6
'       Completed iteration 89
'       Beginning iteration 90
'       Completed iteration 30
'       Beginning iteration 31
'       Break in iteration 8
'       Completed iteration 8
'       Completed iteration 6
'       Beginning iteration 7
'       Completed iteration 7
'
'       Lowest Break Iteration: 8

由于循环的迭代在调用 方法时 Break 仍可能执行,因此每次迭代都会调用 ShouldExitCurrentIteration 属性来检查另一个迭代是否调用 Break 了 方法。 如果属性值为 true,则迭代会检查属性的值 LowestBreakIteration ,如果该值大于当前迭代的索引值,则立即返回 。

注解

请注意,无法实例化此类的实例。 它由编译器作为对 或 Parallel.ForEach 方法的调用Parallel.For中的参数自动生成。 说明如示例所示。

C#) ForFor Each 中的 和 foreach (和 Visual Basic 中的 (等for构造) 按顺序从最低索引到最高索引或从集合中的第一个对象执行到最后一个对象。 相比之下, Parallel.ForParallel.ForEach 方法则不然。 由于循环的各个迭代并行运行,因此可以按任意顺序开始和结束。 类 ParallelLoopState 允许并行循环的各个迭代彼此交互。 类 ParallelLoopState 允许你:

  • 通过调用 Stop 方法退出当前迭代并阻止任何其他迭代启动。 这不会影响已开始执行的迭代。

  • 通过调用 Break 方法防止执行索引大于当前索引的任何迭代。 这不会影响已开始执行的迭代。

  • 通过检索 属性的值 IsExceptional ,确定任何循环迭代中是否发生了异常。

  • 通过检索 属性的值,确定循环的任何迭代是否调用 StopIsStopped 方法。 可以使用此属性从调用 方法之前 Stop 开始但仍在执行的循环迭代中返回。

  • 确定循环的任何迭代是否已调用 BreakStop 方法,或者是否通过检索 属性的值 ShouldExitCurrentIteration 引发了异常。

  • 退出索引大于通过检索 LowestBreakIteration 属性值调用 Break 的迭代的迭代的索引的长时间运行的迭代。

属性

IsExceptional

获取循环的任何迭代是否已引发相应迭代未处理的异常。

IsStopped

获取循环的任何迭代是否已调用 Stop() 方法。

LowestBreakIteration

获取从中调用 Break() 的最低循环迭代。

ShouldExitCurrentIteration

获取循环的当前迭代是否应基于此迭代或其他迭代发出的请求退出。

方法

Break()

告知 Parallel 循环应在系统方便的时候尽早停止执行当前迭代之外的迭代。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
Stop()

告知 Parallel 循环应在系统方便的时候尽早停止执行。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

线程安全性

ParallelLoopState实例仅在提供实例的循环主体内使用。 在关联的循环结束后使用提供的 ParallelLoopState 实例是不安全的,也不安全地将其显式移交给其他线程并让这些线程随时访问它。 将为循环中涉及的每个线程提供不同的 ParallelLoopState 实例。

另请参阅