ParallelLoopState 클래스

정의

병렬 루프의 반복이 다른 반복과 상호 작용할 수 있도록 합니다.Enables iterations of parallel loops to interact with other iterations. 이 클래스의 인스턴스는 Parallel 클래스에 의해 각 루프에 제공됩니다. 코드에 인스턴스를 만들 수는 없습니다.An instance of this class is provided by the Parallel class to each loop; you can not create instances in your code.

public ref class ParallelLoopState
public class ParallelLoopState
type ParallelLoopState = class
Public Class ParallelLoopState
상속
ParallelLoopState

예제

다음 예제에서는 동시에 최대 100 개의 루프 반복을 실행합니다.The following example executes up to 100 iterations of a loop in parallel. 1에서 1,000 밀리초 임의의 간격에 대 한 각 반복 일시 중지합니다.Each iteration pauses for a random interval from 1 to 1,000 milliseconds. 임의로 생성 된 값은 Break 메서드가 호출 되는 루프의 반복을 결정 합니다.A randomly generated value determines on which iteration of the loop the Break method is called. 예제의 출력에 표시 된 것 처럼, 해당 인덱스가 LowestBreakIteration 속성 값 보다 큰 반복은 Break 메서드를 호출한 후에 시작 되지 않습니다.As the output from the example shows, no iterations whose index is greater than the LowestBreakIteration property value start after the call to the Break method.

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);
      Nullable<long> lowest = new Nullable<long>();

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

      var result = Parallel.For(1, 101, (i, state) => {
                                            Console.WriteLine("Beginning iteration {0}", i);
                                            int delay;
                                            Monitor.Enter(rnd);
                                               delay = rnd.Next(1, 1001);
                                            Monitor.Exit(rnd);
                                            Thread.Sleep(delay);
                                            
                                            if (state.ShouldExitCurrentIteration) {
                                               if (state.LowestBreakIteration < i)
                                                  return;
                                            }

                                            if (i == breakIndex) {
                                               Console.WriteLine("Break in iteration {0}", i);
                                               state.Break();
                                               if (state.LowestBreakIteration.HasValue)
                                                  if (lowest < state.LowestBreakIteration)
                                                     lowest = state.LowestBreakIteration;
                                                  else
                                                     lowest = state.LowestBreakIteration;
                                            }

                                            Console.WriteLine("Completed iteration {0}", i);
                                       });
         if (lowest.HasValue)
            Console.WriteLine("\nLowest Break Iteration: {0}", lowest);
         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 메서드를 호출 했는지 여부를 확인 합니다.Because iterations of the loop are still likely to be executing when the Break method is called, each iteration calls the ShouldExitCurrentIteration property to check whether another iteration has called the Break method. 속성 값이 true이면 반복에서 LowestBreakIteration 속성의 값을 확인 하 고, 현재 반복의 인덱스 값 보다 크면를 즉시 반환 합니다.If the property value is true, the iteration checks the value of the LowestBreakIteration property and, if it is greater than the current iteration's index value, returns immediately.

설명

이 클래스의 인스턴스를 인스턴스화할 수 없습니다는 참고 합니다.Note that you cannot instantiate an instance of this class. Parallel.For 또는 Parallel.ForEach 메서드에 대 한 호출에서 컴파일러에 의해 자동으로 생성 됩니다.It is automatically generated by the compiler as an argument in a call to the Parallel.For or Parallel.ForEach method. 예제에서는 그림을 제공합니다.The example provides an illustration.

forforeach (의 C#경우) 및 ForFor Each (Visual Basic)는 가장 낮은 인덱스에서 가장 낮은 인덱스부터 가장 높은 인덱스 또는 마지막으로 설정 된의 첫 번째 개체에서 순차적으로 실행 됩니다.Constructs such as for and foreach (in C#) and For and For Each (in Visual Basic) execute sequentially from the lowest index to the highest or from the first object in a set to the last. 이와 대조적으로 Parallel.ForParallel.ForEach 메서드는 그렇지 않습니다.In contrast, the Parallel.For and Parallel.ForEach methods do not. 루프의 개별 반복을 병렬로 실행할 때문에 시작할 수 있으며 순서에 관계 없이 종료.Because individual iterations of the loop run in parallel, they can begin and end in any order. ParallelLoopState 클래스를 사용 하면 병렬 루프의 개별 반복이 서로 상호 작용할 수 있습니다.The ParallelLoopState class allows individual iterations of parallel loops to interact with one another. ParallelLoopState 클래스를 사용 하 여 다음을 수행할 수 있습니다.The ParallelLoopState class allows you to:

  • 현재 반복을 종료 하 고 Stop 메서드를 호출 하 여 추가 반복이 시작 되지 않도록 합니다.Exit the current iteration and prevent any additional iterations from starting by calling the Stop method. 실행을 이미 시작 하는 반복에는 영향을 주지 않습니다.This does not affect iterations that have already begun execution.

  • Break 메서드를 호출 하 여 현재 인덱스 보다 큰 인덱스의 반복을 실행 하지 않도록 합니다.Prevent any iterations with an index greater than the current index from executing by calling the Break method. 실행을 이미 시작 하는 반복에는 영향을 주지 않습니다.This does not affect iterations that have already begun execution.

  • IsExceptional 속성의 값을 검색 하 여 루프 반복에서 예외가 발생 했는지 여부를 확인 합니다.Determine whether an exception has occurred in any loop iteration by retrieving the value of the IsExceptional property.

  • IsStopped 속성의 값을 검색 하 여 루프 반복에서 Stop 메서드를 호출 했는지 여부를 확인 합니다.Determine whether any iteration of the loop has called the Stop method by retrieving the value of the IsStopped property. 이 속성을 사용 하 여 Stop 메서드를 호출 하기 전에 시작 되었지만 아직 실행 중인 루프의 반복에서 반환 합니다.You can use this property to return from iterations of the loop that started before the call to the Stop method but are still executing.

  • 루프 반복에서 Break 또는 Stop 메서드를 호출 했는지 또는 ShouldExitCurrentIteration 속성의 값을 검색 하 여 예외를 throw 했는지 여부를 확인 합니다.Determine whether any iteration of the loop has called the Break or Stop method or has thrown an exception by retrieving the value of the ShouldExitCurrentIteration property.

  • 인덱스가 LowestBreakIteration 속성의 값을 검색 하 여 Break가 호출 된 반복의 인덱스 보다 큰 장기 실행 반복에서 종료 합니다.Exit from a long-running iteration whose index is greater than the index of an iteration in which Break was called by retrieving the value of the LowestBreakIteration property.

속성

IsExceptional

루프 반복이 해당 반복에서 처리되지 않은 예외를 throw했는지 여부를 가져옵니다.Gets whether any iteration of the loop has thrown an exception that went unhandled by that iteration.

IsStopped

루프 반복에서 Stop() 메서드를 호출했는지 여부를 가져옵니다.Gets whether any iteration of the loop has called the Stop() method.

LowestBreakIteration

Break()가 호출된 루프의 가장 낮은 반복을 가져옵니다.Gets the lowest iteration of the loop from which Break() was called.

ShouldExitCurrentIteration

루프의 현재 반복이 해당 반복 또는 다른 반복에서 수행한 요청에 따라 종료되어야 하는지 여부를 가져옵니다.Gets whether the current iteration of the loop should exit based on requests made by this or other iterations.

메서드

Break()

시스템의 편의상 빨리 수행하는 현재 반복 이외에 Parallel 루프의 실행이 중단되도록 통신합니다.Communicates that the Parallel loop should cease execution of iterations beyond the current iteration at the system's earliest convenience.

Equals(Object)

지정한 개체가 현재 개체와 같은지를 확인합니다.Determines whether the specified object is equal to the current object.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.Serves as the default hash function.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.Gets the Type of the current instance.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.Creates a shallow copy of the current Object.

(다음에서 상속됨 Object)
Stop()

시스템에서 편의상 빨리 수행하는 반복에서 Parallel 루프의 실행이 중단되도록 통신합니다.Communicates that the Parallel loop should cease execution at the system's earliest convenience.

ToString()

현재 개체를 나타내는 string을 반환합니다.Returns a string that represents the current object.

(다음에서 상속됨 Object)

적용 대상

스레드 보안

ParallelLoopState 인스턴스는 인스턴스가 제공 된 루프의 본문 내 에서만 사용 됩니다.A ParallelLoopState instance is meant to be used only within the body of the loop to which an instance was provided. 연결 된 루프가 종료 된 후에 제공 된 ParallelLoopState 인스턴스를 사용 하는 것은 안전 하지 않으며, 다른 스레드에 명시적으로 전달 하는 것이 안전 하지 않으며, 언제 든 지 해당 스레드에 액세스 하도록 할 수 있습니다.It is not safe to use a provided ParallelLoopState instance after the associated loop ends, nor is it safe to explicitly hand it off to other threads and have those threads access it at any time. 루프와 관련 된 각 스레드에 다른 ParallelLoopState 인스턴스가 제공 됩니다.A different ParallelLoopState instance will be provided to each thread involved in a loop.

추가 정보