WaitHandle.WaitAll WaitHandle.WaitAll WaitHandle.WaitAll WaitHandle.WaitAll Method

Definición

Espera a que todos los elementos de la matriz especificada reciban una señal.Waits for all the elements in the specified array to receive a signal.

Sobrecargas

WaitAll(WaitHandle[], TimeSpan, Boolean) WaitAll(WaitHandle[], TimeSpan, Boolean) WaitAll(WaitHandle[], TimeSpan, Boolean) WaitAll(WaitHandle[], TimeSpan, Boolean)

Espera a que todos los elementos de la matriz especificada reciban una señal; usa un valor TimeSpan para determinar el intervalo de tiempo y especifica si se va a salir del dominio de sincronización antes de finalizar la espera.Waits for all the elements in the specified array to receive a signal, using a TimeSpan value to specify the time interval, and specifying whether to exit the synchronization domain before the wait.

WaitAll(WaitHandle[], Int32, Boolean) WaitAll(WaitHandle[], Int32, Boolean) WaitAll(WaitHandle[], Int32, Boolean) WaitAll(WaitHandle[], Int32, Boolean)

Espera a que todos los elementos de la matriz especificada reciban una señal; usa un valor Int32 para determinar el intervalo de tiempo y especifica si se va a salir del dominio de sincronización antes de finalizar la espera.Waits for all the elements in the specified array to receive a signal, using an Int32 value to specify the time interval and specifying whether to exit the synchronization domain before the wait.

WaitAll(WaitHandle[], TimeSpan) WaitAll(WaitHandle[], TimeSpan) WaitAll(WaitHandle[], TimeSpan) WaitAll(WaitHandle[], TimeSpan)

Espera a que todos los elementos de la matriz especificada reciban una señal, usando un valor TimeSpan para especificar el intervalo de tiempo.Waits for all the elements in the specified array to receive a signal, using a TimeSpan value to specify the time interval.

WaitAll(WaitHandle[], Int32) WaitAll(WaitHandle[], Int32) WaitAll(WaitHandle[], Int32) WaitAll(WaitHandle[], Int32)

Espera a que todos los elementos de la matriz especificada reciban una señal mediante un valor Int32 para especificar el intervalo de tiempo.Waits for all the elements in the specified array to receive a signal, using an Int32 value to specify the time interval.

WaitAll(WaitHandle[]) WaitAll(WaitHandle[]) WaitAll(WaitHandle[]) WaitAll(WaitHandle[])

Espera a que todos los elementos de la matriz especificada reciban una señal.Waits for all the elements in the specified array to receive a signal.

WaitAll(WaitHandle[], TimeSpan, Boolean) WaitAll(WaitHandle[], TimeSpan, Boolean) WaitAll(WaitHandle[], TimeSpan, Boolean) WaitAll(WaitHandle[], TimeSpan, Boolean)

Espera a que todos los elementos de la matriz especificada reciban una señal; usa un valor TimeSpan para determinar el intervalo de tiempo y especifica si se va a salir del dominio de sincronización antes de finalizar la espera.Waits for all the elements in the specified array to receive a signal, using a TimeSpan value to specify the time interval, and specifying whether to exit the synchronization domain before the wait.

public:
 static bool WaitAll(cli::array <System::Threading::WaitHandle ^> ^ waitHandles, TimeSpan timeout, bool exitContext);
public static bool WaitAll (System.Threading.WaitHandle[] waitHandles, TimeSpan timeout, bool exitContext);
static member WaitAll : System.Threading.WaitHandle[] * TimeSpan * bool -> bool
Public Shared Function WaitAll (waitHandles As WaitHandle(), timeout As TimeSpan, exitContext As Boolean) As Boolean

Parámetros

waitHandles
WaitHandle[]

Matriz WaitHandle que contiene los objetos por los que la instancia actual esperará.A WaitHandle array containing the objects for which the current instance will wait. Esta matriz no puede contener varias referencias al mismo objeto.This array cannot contain multiple references to the same object.

timeout
TimeSpan TimeSpan TimeSpan TimeSpan

TimeSpan que representa el número de milisegundos de espera o TimeSpan que representa -1 milisegundos para esperar indefinidamente.A TimeSpan that represents the number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds, to wait indefinitely.

exitContext
Boolean Boolean Boolean Boolean

true para salir del dominio de sincronización del contexto antes de la espera (en caso de encontrarse en un contexto sincronizado) y volver a adquirirlo más tarde; de lo contrario, false.true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it afterward; otherwise, false.

Devoluciones

Es true cuando todos los elementos de waitHandles han recibido una señal; en caso contrario es false.true when every element in waitHandles has received a signal; otherwise false.

Excepciones

El parámetro waitHandles es null.The waitHandles parameter is null.

O bien-or- Uno o varios objetos de la matriz waitHandles son null.One or more of the objects in the waitHandles array is null.

O bien-or- waitHandles es una matriz sin elementos y la versión de .NET Framework es 2.0 o posterior.waitHandles is an array with no elements and the .NET Framework version is 2.0 or later.

La matriz waitHandles contiene elementos que son duplicados.The waitHandles array contains elements that are duplicates.

El número de objetos de waitHandles es mayor de lo que permite el sistema.The number of objects in waitHandles is greater than the system permits.

O bien-or- El atributo STAThreadAttribute se aplica al procedimiento de subproceso para el subproceso actual, y waitHandles contiene más de un elemento.The STAThreadAttribute attribute is applied to the thread procedure for the current thread, and waitHandles contains more than one element.

waitHandles es una matriz sin elementos y la versión de .NET Framework es 1.0 o 1.1.waitHandles is an array with no elements and the .NET Framework version is 1.0 or 1.1.

timeout es un número negativo distinto de -1 milisegundos, que representa un tiempo de espera infinito.timeout is a negative number other than -1 milliseconds, which represents an infinite time-out. O bien-or- timeout es mayor que MaxValue.timeout is greater than MaxValue.

La espera finalizó porque un subproceso se cierra sin liberar una exclusión mutua.The wait terminated because a thread exited without releasing a mutex. Esta excepción no se produce en Windows 98 o en Windows Millennium Edition.This exception is not thrown on Windows 98 or Windows Millennium Edition.

La matriz waitHandles contiene un proxy transparente para un WaitHandle en otro dominio de aplicación.The waitHandles array contains a transparent proxy for a WaitHandle in another application domain.

Ejemplos

El ejemplo de código siguiente muestra cómo usar el grupo de subprocesos para crear y escribir en un grupo de archivos de forma asincrónica.The following code example shows how to use the thread pool to asynchronously create and write to a group of files. Cada operación de escritura se pone en cola como un elemento de trabajo y las señales cuando haya finalizado.Each write operation is queued as a work item and signals when it is finished. El subproceso principal espera a que todos los elementos señalar y, a continuación, se cierra.The main thread waits for all the items to signal and then exits.

using namespace System;
using namespace System::IO;
using namespace System::Security::Permissions;
using namespace System::Threading;

// Maintain state to pass to WriteToFile.
ref class State
{
public:
   String^ fileName;
   array<Byte>^byteArray;
   ManualResetEvent^ manualEvent;
   State( String^ fileName, array<Byte>^byteArray, ManualResetEvent^ manualEvent )
      : fileName( fileName ), byteArray( byteArray ), manualEvent( manualEvent )
   {}

};

ref class Writer
{
private:
   static int workItemCount = 0;
   Writer(){}


public:
   static void WriteToFile( Object^ state )
   {
      int workItemNumber = workItemCount;
      Interlocked::Increment( workItemCount );
      Console::WriteLine( "Starting work item {0}.", workItemNumber.ToString() );
      State^ stateInfo = dynamic_cast<State^>(state);
      FileStream^ fileWriter;
      
      // Create and write to the file.
      try
      {
         fileWriter = gcnew FileStream( stateInfo->fileName,FileMode::Create );
         fileWriter->Write( stateInfo->byteArray, 0, stateInfo->byteArray->Length );
      }
      finally
      {
         if ( fileWriter != nullptr )
         {
            fileWriter->Close();
         }
         
         // Signal main() that the work item has finished.
         Console::WriteLine( "Ending work item {0}.", workItemNumber.ToString() );
         stateInfo->manualEvent->Set();
      }

   }

};

int main()
{
   const int numberOfFiles = 5;
   String^ dirName =  "C:\\TestTest";
   String^ fileName;
   array<Byte>^byteArray;
   Random^ randomGenerator = gcnew Random;
   array<ManualResetEvent^>^manualEvents = gcnew array<ManualResetEvent^>(numberOfFiles);
   State^ stateInfo;
   if (  !Directory::Exists( dirName ) )
   {
      Directory::CreateDirectory( dirName );
   }

   
   // Queue the work items that create and write to the files.
   for ( int i = 0; i < numberOfFiles; i++ )
   {
      fileName = String::Concat( dirName,  "\\Test", ((i)).ToString(),  ".dat" );
      
      // Create random data to write to the file.
      byteArray = gcnew array<Byte>(1000000);
      randomGenerator->NextBytes( byteArray );
      manualEvents[ i ] = gcnew ManualResetEvent( false );
      stateInfo = gcnew State( fileName,byteArray,manualEvents[ i ] );
      ThreadPool::QueueUserWorkItem( gcnew WaitCallback( &Writer::WriteToFile ), stateInfo );

   }
   
   // Since ThreadPool threads are background threads, 
   // wait for the work items to signal before exiting.
   if ( WaitHandle::WaitAll( manualEvents, TimeSpan(0,0,5), false ) )
   {
      Console::WriteLine( "Files written - main exiting." );
   }
   else
   {
      
      // The wait operation times out.
      Console::WriteLine( "Error writing files - main exiting." );
   }
}

using System;
using System.IO;
using System.Security.Permissions;
using System.Threading;

class Test
{
    static void Main()
    {
        const int numberOfFiles = 5;
        string dirName = @"C:\TestTest";
        string fileName;

        byte[] byteArray;
        Random randomGenerator = new Random();

        ManualResetEvent[] manualEvents = 
            new ManualResetEvent[numberOfFiles];
        State stateInfo;

        if(!Directory.Exists(dirName))
        {
            Directory.CreateDirectory(dirName);
        }

        // Queue the work items that create and write to the files.
        for(int i = 0; i < numberOfFiles; i++)
        {
            fileName = string.Concat(
                dirName, @"\Test", i.ToString(), ".dat");

            // Create random data to write to the file.
            byteArray = new byte[1000000];
            randomGenerator.NextBytes(byteArray);

            manualEvents[i] = new ManualResetEvent(false);

            stateInfo = 
                new State(fileName, byteArray, manualEvents[i]);

            ThreadPool.QueueUserWorkItem(new WaitCallback(
                Writer.WriteToFile), stateInfo);
        }
    
        // Since ThreadPool threads are background threads, 
        // wait for the work items to signal before exiting.
        if(WaitHandle.WaitAll(
            manualEvents, new TimeSpan(0, 0, 5), false))
        {
            Console.WriteLine("Files written - main exiting.");
        }
        else
        {
            // The wait operation times out.
            Console.WriteLine("Error writing files - main exiting.");
        }
    }
}

// Maintain state to pass to WriteToFile.
class State
{
    public string fileName;
    public byte[] byteArray;
    public ManualResetEvent manualEvent;

    public State(string fileName, byte[] byteArray, 
        ManualResetEvent manualEvent)
    {
        this.fileName = fileName;
        this.byteArray = byteArray;
        this.manualEvent = manualEvent;
    }
}

class Writer
{
    static int workItemCount = 0;
    Writer() {}

    public static void WriteToFile(object state)
    {
        int workItemNumber = workItemCount;
        Interlocked.Increment(ref workItemCount);
        Console.WriteLine("Starting work item {0}.",
            workItemNumber.ToString());
        State stateInfo = (State)state;
        FileStream fileWriter = null;

        // Create and write to the file.
        try
        {
            fileWriter = new FileStream(
                stateInfo.fileName, FileMode.Create);
            fileWriter.Write(stateInfo.byteArray, 
                0, stateInfo.byteArray.Length);
        }
        finally
        {
            if(fileWriter != null)
            {
                fileWriter.Close();
            }

            // Signal Main that the work item has finished.
            Console.WriteLine("Ending work item {0}.", 
                workItemNumber.ToString());
            stateInfo.manualEvent.Set();
        }
    }
}
Imports System
Imports System.IO
Imports System.Security.Permissions
Imports System.Threading

Public Class Test

    ' WaitHandle.WaitAll requires a multithreaded apartment 
    ' when using multiple wait handles.
    <MTAThreadAttribute> _
    Shared Sub Main()
        Const numberOfFiles As Integer = 5
        Dim dirName As String = "C:\TestTest"
        Dim fileName As String 

        Dim byteArray() As Byte 
        Dim randomGenerator As New Random()

        Dim manualEvents(numberOfFiles - 1) As ManualResetEvent
        Dim stateInfo As State 

        If Directory.Exists(dirName) <> True Then
            Directory.CreateDirectory(dirName)
        End If

        ' Queue the work items that create and write to the files.
        For i As Integer = 0 To numberOfFiles - 1
            fileName = String.Concat( _
                dirName, "\Test", i.ToString(), ".dat")

            ' Create random data to write to the file.
            byteArray = New Byte(1000000){}
            randomGenerator.NextBytes(byteArray)

            manualEvents(i) = New ManualResetEvent(false)

            stateInfo = _ 
                New State(fileName, byteArray, manualEvents(i))

            ThreadPool.QueueUserWorkItem(AddressOf _
                Writer.WriteToFile, stateInfo)
        Next i
    
        ' Since ThreadPool threads are background threads, 
        ' wait for the work items to signal before exiting.
        If WaitHandle.WaitAll( _
            manualEvents, New TimeSpan(0, 0, 5), false) = True  Then

            Console.WriteLine("Files written - main exiting.")
        Else
        
            ' The wait operation times out.
            Console.WriteLine("Error writing files - main exiting.")
        End If
    End Sub

End Class
 
' Maintain state to pass to WriteToFile.
Public Class State

    Public fileName As String
    Public byteArray As Byte()
    Public manualEvent As ManualResetEvent

    Sub New(fileName As String, byteArray() As Byte, _
        manualEvent As ManualResetEvent)
    
        Me.fileName = fileName
        Me.byteArray = byteArray
        Me.manualEvent = manualEvent
    End Sub

End Class

Public Class Writer

    Private Sub New()
    End Sub

    Shared workItemCount As Integer = 0

    Shared Sub WriteToFile(state As Object)
        Dim workItemNumber As Integer = workItemCount
        Interlocked.Increment(workItemCount)
        Console.WriteLine("Starting work item {0}.", _
            workItemNumber.ToString())
        Dim stateInfo As State = CType(state, State)
        Dim fileWriter As FileStream = Nothing

        ' Create and write to the file.
        Try
            fileWriter = New FileStream( _
                stateInfo.fileName, FileMode.Create)
            fileWriter.Write(stateInfo.byteArray, _
                0, stateInfo.byteArray.Length)
        Finally
            If Not fileWriter Is Nothing Then
                fileWriter.Close()
            End If

            ' Signal Main that the work item has finished.
            Console.WriteLine("Ending work item {0}.", _
                workItemNumber.ToString())
            stateInfo.manualEvent.Set()
        End Try
    End Sub

End Class

Comentarios

Si timeout es cero, el método no se bloquea.If timeout is zero, the method does not block. Comprueba el estado de los identificadores de espera y vuelve inmediatamente.It tests the state of the wait handles and returns immediately.

AbandonedMutexException Novedades de la versión 2.0 de .NET Framework.AbandonedMutexException is new in the .NET Framework version 2.0. En versiones anteriores, el WaitAll devuelve del método true cuando se abandona una exclusión mutua.In previous versions, the WaitAll method returns true when a mutex is abandoned. A menudo, una exclusión mutua abandonada indica un error grave de codificación.An abandoned mutex often indicates a serious coding error. En el caso de una exclusión mutua todo el sistema, podría indicar que una aplicación ha finalizado abruptamente (por ejemplo, mediante el Administrador de tareas de Windows).In the case of a system-wide mutex, it might indicate that an application has been terminated abruptly (for example, by using Windows Task Manager). La excepción contiene información útil para la depuración.The exception contains information useful for debugging.

El WaitAll método devuelve cuando finaliza la espera, lo que significa que todos los identificadores están señalados o se produce un tiempo de espera.The WaitAll method returns when the wait terminates, which means either all the handles are signaled or a time-out occurs. En algunas implementaciones, si se pasan más de 64 identificadores, un NotSupportedException se produce.On some implementations, if more than 64 handles are passed, a NotSupportedException is thrown. Si la matriz contiene duplicados, se producirá un error en la llamada.If the array contains duplicates, the call will fail.

Nota

El WaitAll método no se admite en subprocesos que tienen STAThreadAttribute.The WaitAll method is not supported on threads that have STAThreadAttribute.

El valor máximo de timeout es Int32.MaxValue.The maximum value for timeout is Int32.MaxValue.

Notas sobre la salida del contextoNotes on Exiting the Context

El exitContext parámetro no tiene ningún efecto a menos que el WaitAll método se llama desde dentro de un contexto administrado no predeterminado.The exitContext parameter has no effect unless the WaitAll method is called from inside a nondefault managed context. Esto puede ocurrir si el subproceso está dentro de una llamada a una instancia de una clase derivada de ContextBoundObject.This can happen if your thread is inside a call to an instance of a class derived from ContextBoundObject. Incluso si se están ejecutando un método en una clase que no se deriva ContextBoundObject, como String, puede estar en un contexto no predeterminado si un ContextBoundObject en la pila en el dominio de aplicación actual.Even if you are currently executing a method on a class that is not derived from ContextBoundObject, like String, you can be in a nondefault context if a ContextBoundObject is on your stack in the current application domain.

Cuando el código se ejecuta en un contexto no predeterminado, especificar true para exitContext hace que el subproceso salga del contexto administrado no predeterminado (es decir, para realizar la transición al contexto predeterminado) antes de ejecutar el WaitAll método.When your code is executing in a nondefault context, specifying true for exitContext causes the thread to exit the nondefault managed context (that is, to transition to the default context) before executing the WaitAll method. Devuelve al contexto original no predeterminado después de llamar a la WaitAll método se completa.It returns to the original nondefault context after the call to the WaitAll method completes.

Esto puede ser útil cuando la clase de contexto enlazado tiene SynchronizationAttribute.This can be useful when the context-bound class has SynchronizationAttribute. En ese caso, todas las llamadas a miembros de la clase se sincronizan automáticamente y el dominio de sincronización es todo el cuerpo de código para la clase.In that case, all calls to members of the class are automatically synchronized, and the synchronization domain is the entire body of code for the class. Si llama código en la pila de llamadas de un miembro de la WaitAll método y especifica true para exitContext, el subproceso sale del dominio de sincronización, lo que permite un subproceso que esté bloqueado en una llamada a cualquier miembro del objeto para continuar.If code in the call stack of a member calls the WaitAll method and specifies true for exitContext, the thread exits the synchronization domain, allowing a thread that is blocked on a call to any member of the object to proceed. Cuando el WaitAll método finaliza, el subproceso que realizó la llamada debe esperar para volver a escribir el dominio de sincronización.When the WaitAll method returns, the thread that made the call must wait to reenter the synchronization domain.

WaitAll(WaitHandle[], Int32, Boolean) WaitAll(WaitHandle[], Int32, Boolean) WaitAll(WaitHandle[], Int32, Boolean) WaitAll(WaitHandle[], Int32, Boolean)

Espera a que todos los elementos de la matriz especificada reciban una señal; usa un valor Int32 para determinar el intervalo de tiempo y especifica si se va a salir del dominio de sincronización antes de finalizar la espera.Waits for all the elements in the specified array to receive a signal, using an Int32 value to specify the time interval and specifying whether to exit the synchronization domain before the wait.

public:
 static bool WaitAll(cli::array <System::Threading::WaitHandle ^> ^ waitHandles, int millisecondsTimeout, bool exitContext);
public static bool WaitAll (System.Threading.WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext);
static member WaitAll : System.Threading.WaitHandle[] * int * bool -> bool
Public Shared Function WaitAll (waitHandles As WaitHandle(), millisecondsTimeout As Integer, exitContext As Boolean) As Boolean

Parámetros

waitHandles
WaitHandle[]

Matriz WaitHandle que contiene los objetos por los que la instancia actual esperará.A WaitHandle array containing the objects for which the current instance will wait. Esta matriz no puede contener varias referencias al mismo objeto (duplicados).This array cannot contain multiple references to the same object (duplicates).

millisecondsTimeout
Int32 Int32 Int32 Int32

Número de milisegundos de espera o Infinite (-1) para esperar indefinidamente.The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.

exitContext
Boolean Boolean Boolean Boolean

true para salir del dominio de sincronización del contexto antes de la espera (en caso de encontrarse en un contexto sincronizado) y volver a adquirirlo más tarde; de lo contrario, false.true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it afterward; otherwise, false.

Devoluciones

true cuando todos los elementos de waitHandles reciben una señal; en caso contrario, false.true when every element in waitHandles has received a signal; otherwise, false.

Excepciones

El parámetro waitHandles es null.The waitHandles parameter is null.

O bien-or- Uno o varios objetos de la matriz waitHandles son null.One or more of the objects in the waitHandles array is null.

O bien-or- waitHandles es una matriz sin elementos y la versión de .NET Framework es 2.0 o posterior.waitHandles is an array with no elements and the .NET Framework version is 2.0 or later.

La matriz waitHandles contiene elementos que son duplicados.The waitHandles array contains elements that are duplicates.

El número de objetos de waitHandles es mayor de lo que permite el sistema.The number of objects in waitHandles is greater than the system permits.

O bien-or- El atributo STAThreadAttribute se aplica al procedimiento de subproceso para el subproceso actual, y waitHandles contiene más de un elemento.The STAThreadAttribute attribute is applied to the thread procedure for the current thread, and waitHandles contains more than one element.

waitHandles es una matriz sin elementos y la versión de .NET Framework es 1.0 o 1.1.waitHandles is an array with no elements and the .NET Framework version is 1.0 or 1.1.

millisecondsTimeout es un número negativo distinto de-1, que representa un tiempo de espera infinito.millisecondsTimeout is a negative number other than -1, which represents an infinite time-out.

La espera finalizó porque un subproceso se cierra sin liberar una exclusión mutua.The wait completed because a thread exited without releasing a mutex. Esta excepción no se produce en Windows 98 o en Windows Millennium Edition.This exception is not thrown on Windows 98 or Windows Millennium Edition.

La matriz waitHandles contiene un proxy transparente para un WaitHandle en otro dominio de aplicación.The waitHandles array contains a transparent proxy for a WaitHandle in another application domain.

Ejemplos

El ejemplo de código siguiente muestra cómo usar el grupo de subprocesos para crear y escribir en un grupo de archivos de forma asincrónica.The following code example shows how to use the thread pool to asynchronously create and write to a group of files. Cada operación de escritura se pone en cola como un elemento de trabajo y las señales cuando haya finalizado.Each write operation is queued as a work item and signals when it is finished. El subproceso principal espera a que todos los elementos señalar y, a continuación, se cierra.The main thread waits for all the items to signal and then exits.

using namespace System;
using namespace System::IO;
using namespace System::Security::Permissions;
using namespace System::Threading;

// Maintain state to pass to WriteToFile.
ref class State
{
public:
   String^ fileName;
   array<Byte>^byteArray;
   ManualResetEvent^ manualEvent;
   State( String^ fileName, array<Byte>^byteArray, ManualResetEvent^ manualEvent )
      : fileName( fileName ), byteArray( byteArray ), manualEvent( manualEvent )
   {}

};

ref class Writer
{
private:
   static int workItemCount = 0;
   Writer(){}


public:
   static void WriteToFile( Object^ state )
   {
      int workItemNumber = workItemCount;
      Interlocked::Increment( workItemCount );
      Console::WriteLine( "Starting work item {0}.", workItemNumber.ToString() );
      State^ stateInfo = dynamic_cast<State^>(state);
      FileStream^ fileWriter;
      
      // Create and write to the file.
      try
      {
         fileWriter = gcnew FileStream( stateInfo->fileName,FileMode::Create );
         fileWriter->Write( stateInfo->byteArray, 0, stateInfo->byteArray->Length );
      }
      finally
      {
         if ( fileWriter != nullptr )
         {
            fileWriter->Close();
         }
         
         // Signal main() that the work item has finished.
         Console::WriteLine( "Ending work item {0}.", workItemNumber.ToString() );
         stateInfo->manualEvent->Set();
      }

   }

};

int main()
{
   const int numberOfFiles = 5;
   String^ dirName =  "C:\\TestTest";
   String^ fileName;
   array<Byte>^byteArray;
   Random^ randomGenerator = gcnew Random;
   array<ManualResetEvent^>^manualEvents = gcnew array<ManualResetEvent^>(numberOfFiles);
   State^ stateInfo;
   if (  !Directory::Exists( dirName ) )
   {
      Directory::CreateDirectory( dirName );
   }

   
   // Queue the work items that create and write to the files.
   for ( int i = 0; i < numberOfFiles; i++ )
   {
      fileName = String::Concat( dirName,  "\\Test", ((i)).ToString(),  ".dat" );
      
      // Create random data to write to the file.
      byteArray = gcnew array<Byte>(1000000);
      randomGenerator->NextBytes( byteArray );
      manualEvents[ i ] = gcnew ManualResetEvent( false );
      stateInfo = gcnew State( fileName,byteArray,manualEvents[ i ] );
      ThreadPool::QueueUserWorkItem( gcnew WaitCallback( &Writer::WriteToFile ), stateInfo );

   }
   
   // Since ThreadPool threads are background threads, 
   // wait for the work items to signal before exiting.
   if ( WaitHandle::WaitAll( manualEvents, 5000, false ) )
   {
      Console::WriteLine( "Files written - main exiting." );
   }
   else
   {
      
      // The wait operation times out.
      Console::WriteLine( "Error writing files - main exiting." );
   }
}

using System;
using System.IO;
using System.Security.Permissions;
using System.Threading;

class Test
{
    static void Main()
    {
        const int numberOfFiles = 5;
        string dirName = @"C:\TestTest";
        string fileName;

        byte[] byteArray;
        Random randomGenerator = new Random();

        ManualResetEvent[] manualEvents = 
            new ManualResetEvent[numberOfFiles];
        State stateInfo;

        if(!Directory.Exists(dirName))
        {
            Directory.CreateDirectory(dirName);
        }

        // Queue the work items that create and write to the files.
        for(int i = 0; i < numberOfFiles; i++)
        {
            fileName = string.Concat(
                dirName, @"\Test", i.ToString(), ".dat");

            // Create random data to write to the file.
            byteArray = new byte[1000000];
            randomGenerator.NextBytes(byteArray);

            manualEvents[i] = new ManualResetEvent(false);

            stateInfo = 
                new State(fileName, byteArray, manualEvents[i]);

            ThreadPool.QueueUserWorkItem(new WaitCallback(
                Writer.WriteToFile), stateInfo);
        }
    
        // Since ThreadPool threads are background threads, 
        // wait for the work items to signal before exiting.
        if(WaitHandle.WaitAll(manualEvents, 5000, false))
        {
            Console.WriteLine("Files written - main exiting.");
        }
        else
        {
            // The wait operation times out.
            Console.WriteLine("Error writing files - main exiting.");
        }
    }
}

// Maintain state to pass to WriteToFile.
class State
{
    public string fileName;
    public byte[] byteArray;
    public ManualResetEvent manualEvent;

    public State(string fileName, byte[] byteArray, 
        ManualResetEvent manualEvent)
    {
        this.fileName = fileName;
        this.byteArray = byteArray;
        this.manualEvent = manualEvent;
    }
}

class Writer
{
    static int workItemCount = 0;
    Writer() {}

    public static void WriteToFile(object state)
    {
        int workItemNumber = workItemCount;
        Interlocked.Increment(ref workItemCount);
        Console.WriteLine("Starting work item {0}.",
            workItemNumber.ToString());
        State stateInfo = (State)state;
        FileStream fileWriter = null;

        // Create and write to the file.
        try
        {
            fileWriter = new FileStream(
                stateInfo.fileName, FileMode.Create);
            fileWriter.Write(stateInfo.byteArray, 
                0, stateInfo.byteArray.Length);
        }
        finally
        {
            if(fileWriter != null)
            {
                fileWriter.Close();
            }

            // Signal Main that the work item has finished.
            Console.WriteLine("Ending work item {0}.", 
                workItemNumber.ToString());
            stateInfo.manualEvent.Set();
        }
    }
}
Imports System
Imports System.IO
Imports System.Security.Permissions
Imports System.Threading

Public Class Test

    ' WaitHandle.WaitAll requires a multithreaded apartment 
    ' when using multiple wait handles.
    <MTAThreadAttribute> _
    Shared Sub Main()
        Const numberOfFiles As Integer = 5
        Dim dirName As String = "C:\TestTest"
        Dim fileName As String 

        Dim byteArray() As Byte 
        Dim randomGenerator As New Random()

        Dim manualEvents(numberOfFiles - 1) As ManualResetEvent
        Dim stateInfo As State 

        If Directory.Exists(dirName) <> True Then
            Directory.CreateDirectory(dirName)
        End If

        ' Queue the work items that create and write to the files.
        For i As Integer = 0 To numberOfFiles - 1
            fileName = String.Concat( _
                dirName, "\Test", i.ToString(), ".dat")

            ' Create random data to write to the file.
            byteArray = New Byte(1000000){}
            randomGenerator.NextBytes(byteArray)

            manualEvents(i) = New ManualResetEvent(false)

            stateInfo = _ 
                New State(fileName, byteArray, manualEvents(i))

            ThreadPool.QueueUserWorkItem(AddressOf _
                Writer.WriteToFile, stateInfo)
        Next i
    
        ' Since ThreadPool threads are background threads, 
        ' wait for the work items to signal before exiting.
        If WaitHandle.WaitAll(manualEvents, 5000, false) = True  Then

            Console.WriteLine("Files written - main exiting.")
        Else
        
            ' The wait operation times out.
            Console.WriteLine("Error writing files - main exiting.")
        End If
    End Sub

End Class
 
' Maintain state to pass to WriteToFile.
Public Class State

    Public fileName As String
    Public byteArray As Byte()
    Public manualEvent As ManualResetEvent

    Sub New(fileName As String, byteArray() As Byte, _
        manualEvent As ManualResetEvent)
    
        Me.fileName = fileName
        Me.byteArray = byteArray
        Me.manualEvent = manualEvent
    End Sub

End Class

Public Class Writer

    Private Sub New()
    End Sub

    Shared workItemCount As Integer = 0

    Shared Sub WriteToFile(state As Object)
        Dim workItemNumber As Integer = workItemCount
        Interlocked.Increment(workItemCount)
        Console.WriteLine("Starting work item {0}.", _
            workItemNumber.ToString())
        Dim stateInfo As State = CType(state, State)
        Dim fileWriter As FileStream = Nothing

        ' Create and write to the file.
        Try
            fileWriter = New FileStream( _
                stateInfo.fileName, FileMode.Create)
            fileWriter.Write(stateInfo.byteArray, _
                0, stateInfo.byteArray.Length)
        Finally
            If Not fileWriter Is Nothing Then
                fileWriter.Close()
            End If

            ' Signal Main that the work item has finished.
            Console.WriteLine("Ending work item {0}.", _
                workItemNumber.ToString())
            stateInfo.manualEvent.Set()
        End Try
    End Sub

End Class

Comentarios

Si millisecondsTimeout es cero, el método no se bloquea.If millisecondsTimeout is zero, the method does not block. Comprueba el estado de los identificadores de espera y vuelve inmediatamente.It tests the state of the wait handles and returns immediately.

AbandonedMutexException Novedades de la versión 2.0 de .NET Framework.AbandonedMutexException is new in the .NET Framework version 2.0. En versiones anteriores, el WaitAll devuelve del método true cuando se abandona una exclusión mutua.In previous versions, the WaitAll method returns true when a mutex is abandoned. A menudo, una exclusión mutua abandonada indica un error grave de codificación.An abandoned mutex often indicates a serious coding error. En el caso de una exclusión mutua todo el sistema, podría indicar que una aplicación ha finalizado abruptamente (por ejemplo, mediante el Administrador de tareas de Windows).In the case of a system-wide mutex, it might indicate that an application has been terminated abruptly (for example, by using Windows Task Manager). La excepción contiene información útil para la depuración.The exception contains information useful for debugging.

El WaitAll método devuelve cuando finaliza la espera, lo que significa que cuando están señalados todos los identificadores o cuando se produce el tiempo de espera.The WaitAll method returns when the wait terminates, which means either when all the handles are signaled or when time-out occurs. En algunas implementaciones, si se pasan más de 64 identificadores, un NotSupportedException se produce.On some implementations, if more than 64 handles are passed, a NotSupportedException is thrown. Si hay duplicados en la matriz, no se puede llamar con un DuplicateWaitObjectException.If there are duplicates in the array, the call fails with a DuplicateWaitObjectException.

Nota

El WaitAll método no se admite en subprocesos que tienen STAThreadAttribute.The WaitAll method is not supported on threads that have STAThreadAttribute.

Notas sobre la salida del contextoNotes on Exiting the Context

El exitContext parámetro no tiene ningún efecto a menos que el WaitAll método se llama desde dentro de un contexto administrado no predeterminado.The exitContext parameter has no effect unless the WaitAll method is called from inside a nondefault managed context. Esto puede ocurrir si el subproceso está dentro de una llamada a una instancia de una clase derivada de ContextBoundObject.This can happen if your thread is inside a call to an instance of a class derived from ContextBoundObject. Incluso si se están ejecutando un método en una clase que no se deriva ContextBoundObject, como String, puede estar en un contexto no predeterminado si un ContextBoundObject en la pila en el dominio de aplicación actual.Even if you are currently executing a method on a class that is not derived from ContextBoundObject, like String, you can be in a nondefault context if a ContextBoundObject is on your stack in the current application domain.

Cuando el código se ejecuta en un contexto no predeterminado, especificar true para exitContext hace que el subproceso salga del contexto administrado no predeterminado (es decir, para realizar la transición al contexto predeterminado) antes de ejecutar el WaitAll método.When your code is executing in a nondefault context, specifying true for exitContext causes the thread to exit the nondefault managed context (that is, to transition to the default context) before executing the WaitAll method. El subproceso vuelve al contexto original no predeterminado después de llamar a la WaitAll método se completa.The thread returns to the original nondefault context after the call to the WaitAll method completes.

Esto puede ser útil cuando la clase de contexto enlazado tiene el SynchronizationAttribute atributo.This can be useful when the context-bound class has the SynchronizationAttribute attribute. En ese caso, todas las llamadas a miembros de la clase se sincronizan automáticamente y el dominio de sincronización es todo el cuerpo de código para la clase.In that case, all calls to members of the class are automatically synchronized, and the synchronization domain is the entire body of code for the class. Si llama código en la pila de llamadas de un miembro de la WaitAll método y especifica true para exitContext, el subproceso sale del dominio de sincronización, lo que permite un subproceso que esté bloqueado en una llamada a cualquier miembro del objeto para continuar.If code in the call stack of a member calls the WaitAll method and specifies true for exitContext, the thread exits the synchronization domain, allowing a thread that is blocked on a call to any member of the object to proceed. Cuando el WaitAll método finaliza, el subproceso que realizó la llamada debe esperar para volver a escribir el dominio de sincronización.When the WaitAll method returns, the thread that made the call must wait to reenter the synchronization domain.

WaitAll(WaitHandle[], TimeSpan) WaitAll(WaitHandle[], TimeSpan) WaitAll(WaitHandle[], TimeSpan) WaitAll(WaitHandle[], TimeSpan)

Espera a que todos los elementos de la matriz especificada reciban una señal, usando un valor TimeSpan para especificar el intervalo de tiempo.Waits for all the elements in the specified array to receive a signal, using a TimeSpan value to specify the time interval.

public:
 static bool WaitAll(cli::array <System::Threading::WaitHandle ^> ^ waitHandles, TimeSpan timeout);
public static bool WaitAll (System.Threading.WaitHandle[] waitHandles, TimeSpan timeout);
static member WaitAll : System.Threading.WaitHandle[] * TimeSpan -> bool
Public Shared Function WaitAll (waitHandles As WaitHandle(), timeout As TimeSpan) As Boolean

Parámetros

waitHandles
WaitHandle[]

Matriz WaitHandle que contiene los objetos por los que la instancia actual esperará.A WaitHandle array containing the objects for which the current instance will wait. Esta matriz no puede contener varias referencias al mismo objeto.This array cannot contain multiple references to the same object.

timeout
TimeSpan TimeSpan TimeSpan TimeSpan

TimeSpan que representa el número de milisegundos de espera o TimeSpan que representa -1 milisegundos para esperar indefinidamente.A TimeSpan that represents the number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds, to wait indefinitely.

Devoluciones

true cuando todos los elementos de waitHandles reciben una señal; en caso contrario, false.true when every element in waitHandles has received a signal; otherwise, false.

Excepciones

El parámetro waitHandles es null.The waitHandles parameter is null.

O bien-or- Uno o varios objetos de la matriz waitHandles son null.One or more of the objects in the waitHandles array is null.

O bien-or- waitHandles es una matriz sin elementos.waitHandles is an array with no elements.

En .NET para aplicaciones de la Tienda Windows o la Biblioteca de clases portable, capture en su lugar la excepción de clase base, ArgumentException.In the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, ArgumentException, instead.

La matriz waitHandles contiene elementos que son duplicados.The waitHandles array contains elements that are duplicates.

El número de objetos de waitHandles es mayor de lo que permite el sistema.The number of objects in waitHandles is greater than the system permits.

O bien-or- El atributo STAThreadAttribute se aplica al procedimiento de subproceso para el subproceso actual, y waitHandles contiene más de un elemento.The STAThreadAttribute attribute is applied to the thread procedure for the current thread, and waitHandles contains more than one element.

timeout es un número negativo distinto de -1 milisegundos, que representa un tiempo de espera infinito.timeout is a negative number other than -1 milliseconds, which represents an infinite time-out. O bien-or- timeout es mayor que MaxValue.timeout is greater than MaxValue.

La espera finalizó porque un subproceso se cierra sin liberar una exclusión mutua.The wait terminated because a thread exited without releasing a mutex. Esta excepción no se produce en Windows 98 o en Windows Millennium Edition.This exception is not thrown on Windows 98 or Windows Millennium Edition.

La matriz waitHandles contiene un proxy transparente para un WaitHandle en otro dominio de aplicación.The waitHandles array contains a transparent proxy for a WaitHandle in another application domain.

Comentarios

Si timeout es cero, el método no se bloquea.If timeout is zero, the method does not block. Comprueba el estado de los identificadores de espera y vuelve inmediatamente.It tests the state of the wait handles and returns immediately.

El WaitAll método devuelve cuando finaliza la espera, lo que significa que todos los identificadores están señalados o se produce un tiempo de espera.The WaitAll method returns when the wait terminates, which means either all the handles are signaled or a time-out occurs. En algunas implementaciones, si se pasan más de 64 identificadores, un NotSupportedException se produce.On some implementations, if more than 64 handles are passed, a NotSupportedException is thrown. Si la matriz contiene duplicados, se producirá un error en la llamada.If the array contains duplicates, the call will fail.

Nota

El WaitAll método no se admite en subprocesos que tienen STAThreadAttribute.The WaitAll method is not supported on threads that have STAThreadAttribute.

El valor máximo de timeout es Int32.MaxValue.The maximum value for timeout is Int32.MaxValue.

Llamar a esta sobrecarga del método es igual que llamar a la WaitAll(WaitHandle[], TimeSpan, Boolean) sobrecarga y especificando false para exitContext.Calling this method overload is the same as calling the WaitAll(WaitHandle[], TimeSpan, Boolean) overload and specifying false for exitContext.

WaitAll(WaitHandle[], Int32) WaitAll(WaitHandle[], Int32) WaitAll(WaitHandle[], Int32) WaitAll(WaitHandle[], Int32)

Espera a que todos los elementos de la matriz especificada reciban una señal mediante un valor Int32 para especificar el intervalo de tiempo.Waits for all the elements in the specified array to receive a signal, using an Int32 value to specify the time interval.

public:
 static bool WaitAll(cli::array <System::Threading::WaitHandle ^> ^ waitHandles, int millisecondsTimeout);
public static bool WaitAll (System.Threading.WaitHandle[] waitHandles, int millisecondsTimeout);
static member WaitAll : System.Threading.WaitHandle[] * int -> bool
Public Shared Function WaitAll (waitHandles As WaitHandle(), millisecondsTimeout As Integer) As Boolean

Parámetros

waitHandles
WaitHandle[]

Matriz WaitHandle que contiene los objetos por los que la instancia actual esperará.A WaitHandle array containing the objects for which the current instance will wait. Esta matriz no puede contener varias referencias al mismo objeto (duplicados).This array cannot contain multiple references to the same object (duplicates).

millisecondsTimeout
Int32 Int32 Int32 Int32

Número de milisegundos de espera o Infinite (-1) para esperar indefinidamente.The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.

Devoluciones

true cuando todos los elementos de waitHandles reciben una señal; en caso contrario, false.true when every element in waitHandles has received a signal; otherwise, false.

Excepciones

El parámetro waitHandles es null.The waitHandles parameter is null.

O bien-or- Uno o varios objetos de la matriz waitHandles son null.One or more of the objects in the waitHandles array is null.

O bien-or- waitHandles es una matriz sin elementos.waitHandles is an array with no elements.

En .NET para aplicaciones de la Tienda Windows o la Biblioteca de clases portable, capture en su lugar la excepción de clase base, ArgumentException.In the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, ArgumentException, instead.

La matriz waitHandles contiene elementos que son duplicados.The waitHandles array contains elements that are duplicates.

El número de objetos de waitHandles es mayor de lo que permite el sistema.The number of objects in waitHandles is greater than the system permits.

O bien-or- El atributo STAThreadAttribute se aplica al procedimiento de subproceso para el subproceso actual, y waitHandles contiene más de un elemento.The STAThreadAttribute attribute is applied to the thread procedure for the current thread, and waitHandles contains more than one element.

millisecondsTimeout es un número negativo distinto de-1, que representa un tiempo de espera infinito.millisecondsTimeout is a negative number other than -1, which represents an infinite time-out.

La espera finalizó porque un subproceso se cierra sin liberar una exclusión mutua.The wait completed because a thread exited without releasing a mutex. Esta excepción no se produce en Windows 98 o en Windows Millennium Edition.This exception is not thrown on Windows 98 or Windows Millennium Edition.

La matriz waitHandles contiene un proxy transparente para un WaitHandle en otro dominio de aplicación.The waitHandles array contains a transparent proxy for a WaitHandle in another application domain.

Comentarios

Si millisecondsTimeout es cero, el método no se bloquea.If millisecondsTimeout is zero, the method does not block. Comprueba el estado de los identificadores de espera y vuelve inmediatamente.It tests the state of the wait handles and returns immediately.

El WaitAll método devuelve cuando finaliza la espera, lo que significa que cuando están señalados todos los identificadores o cuando se produce el tiempo de espera.The WaitAll method returns when the wait terminates, which means either when all the handles are signaled or when time-out occurs. En algunas implementaciones, si se pasan más de 64 identificadores, un NotSupportedException se produce.On some implementations, if more than 64 handles are passed, a NotSupportedException is thrown. Si hay duplicados en la matriz, no se puede llamar con un DuplicateWaitObjectException.If there are duplicates in the array, the call fails with a DuplicateWaitObjectException.

Nota

El WaitAll método no se admite en subprocesos que tienen STAThreadAttribute.The WaitAll method is not supported on threads that have STAThreadAttribute.

Llamar a esta sobrecarga del método es igual que llamar a la WaitAll(WaitHandle[], Int32, Boolean) sobrecarga y especificando false para exitContext.Calling this method overload is the same as calling the WaitAll(WaitHandle[], Int32, Boolean) overload and specifying false for exitContext.

WaitAll(WaitHandle[]) WaitAll(WaitHandle[]) WaitAll(WaitHandle[]) WaitAll(WaitHandle[])

Espera a que todos los elementos de la matriz especificada reciban una señal.Waits for all the elements in the specified array to receive a signal.

public:
 static bool WaitAll(cli::array <System::Threading::WaitHandle ^> ^ waitHandles);
public static bool WaitAll (System.Threading.WaitHandle[] waitHandles);
static member WaitAll : System.Threading.WaitHandle[] -> bool
Public Shared Function WaitAll (waitHandles As WaitHandle()) As Boolean

Parámetros

waitHandles
WaitHandle[]

Matriz WaitHandle que contiene los objetos por los que la instancia actual esperará.A WaitHandle array containing the objects for which the current instance will wait. Esta matriz no puede contener varias referencias al mismo objeto.This array cannot contain multiple references to the same object.

Devoluciones

true cuando todos los elementos de waitHandles reciben una señal; en caso contrario, el método nunca devuelve ningún valor.true when every element in waitHandles has received a signal; otherwise the method never returns.

Excepciones

El parámetro waitHandles es null.The waitHandles parameter is null. O bien-or- Uno o varios objetos de la matriz waitHandles son null.One or more of the objects in the waitHandles array are null.

O bien-or- waitHandles es una matriz sin elementos y la versión de .NET Framework es 2.0 o posterior.waitHandles is an array with no elements and the .NET Framework version is 2.0 or later.

En .NET para aplicaciones de la Tienda Windows o la Biblioteca de clases portable, capture en su lugar la excepción de clase base, ArgumentException.In the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, ArgumentException, instead.

La matriz waitHandles contiene elementos que son duplicados.The waitHandles array contains elements that are duplicates.

El número de objetos de waitHandles es mayor de lo que permite el sistema.The number of objects in waitHandles is greater than the system permits.

O bien-or- El atributo STAThreadAttribute se aplica al procedimiento de subproceso para el subproceso actual, y waitHandles contiene más de un elemento.The STAThreadAttribute attribute is applied to the thread procedure for the current thread, and waitHandles contains more than one element.

waitHandles es una matriz sin elementos y la versión de .NET Framework es 1.0 o 1.1.waitHandles is an array with no elements and the .NET Framework version is 1.0 or 1.1.

La espera finalizó porque un subproceso se cierra sin liberar una exclusión mutua.The wait terminated because a thread exited without releasing a mutex. Esta excepción no se produce en Windows 98 o en Windows Millennium Edition.This exception is not thrown on Windows 98 or Windows Millennium Edition.

La matriz waitHandles contiene un proxy transparente para un WaitHandle en otro dominio de aplicación.The waitHandles array contains a transparent proxy for a WaitHandle in another application domain.

Ejemplos

El ejemplo de código siguiente muestra cómo usar el grupo de subprocesos para crear y escribir en un grupo de archivos de forma asincrónica.The following code example shows how to use the thread pool to asynchronously create and write to a group of files. Cada operación de escritura se pone en cola como un elemento de trabajo y las señales cuando haya finalizado.Each write operation is queued as a work item and signals when it is finished. El subproceso principal espera a que todos los elementos señalar y, a continuación, se cierra.The main thread waits for all the items to signal and then exits.

using namespace System;
using namespace System::IO;
using namespace System::Security::Permissions;
using namespace System::Threading;

ref class State
{
public:
   String^ fileName;
   array<Byte>^byteArray;
   ManualResetEvent^ manualEvent;
   State( String^ fileName, array<Byte>^byteArray, ManualResetEvent^ manualEvent )
      : fileName( fileName ), byteArray( byteArray ), manualEvent( manualEvent )
   {}

};

ref class Writer
{
private:
   static int workItemCount = 0;
   Writer(){}


public:
   static void WriteToFile( Object^ state )
   {
      int workItemNumber = workItemCount;
      Interlocked::Increment( workItemCount );
      Console::WriteLine( "Starting work item {0}.", workItemNumber.ToString() );
      State^ stateInfo = dynamic_cast<State^>(state);
      FileStream^ fileWriter;
      
      // Create and write to the file.
      try
      {
         fileWriter = gcnew FileStream( stateInfo->fileName,FileMode::Create );
         fileWriter->Write( stateInfo->byteArray, 0, stateInfo->byteArray->Length );
      }
      finally
      {
         if ( fileWriter != nullptr )
         {
            fileWriter->Close();
         }
         
         // Signal main() that the work item has finished.
         Console::WriteLine( "Ending work item {0}.", workItemNumber.ToString() );
         stateInfo->manualEvent->Set();
      }

   }

};

void main()
{
   const int numberOfFiles = 5;
   String^ dirName =  "C:\\TestTest";
   String^ fileName;
   array<Byte>^byteArray;
   Random^ randomGenerator = gcnew Random;
   array<ManualResetEvent^>^manualEvents = gcnew array<ManualResetEvent^>(numberOfFiles);
   State^ stateInfo;
   if (  !Directory::Exists( dirName ) )
   {
      Directory::CreateDirectory( dirName );
   }

   
   // Queue the work items that create and write to the files.
   for ( int i = 0; i < numberOfFiles; i++ )
   {
      fileName = String::Concat( dirName,  "\\Test", ((i)).ToString(),  ".dat" );
      
      // Create random data to write to the file.
      byteArray = gcnew array<Byte>(1000000);
      randomGenerator->NextBytes( byteArray );
      manualEvents[ i ] = gcnew ManualResetEvent( false );
      stateInfo = gcnew State( fileName,byteArray,manualEvents[ i ] );
      ThreadPool::QueueUserWorkItem( gcnew WaitCallback( &Writer::WriteToFile ), stateInfo );

   }
   
   // Since ThreadPool threads are background threads, 
   // wait for the work items to signal before exiting.
   WaitHandle::WaitAll( manualEvents );
   Console::WriteLine( "Files written - main exiting." );
}

using System;
using System.IO;
using System.Security.Permissions;
using System.Threading;

class Test
{
    static void Main()
    {
        const int numberOfFiles = 5;
        string dirName = @"C:\TestTest";
        string fileName;

        byte[] byteArray;
        Random randomGenerator = new Random();

        ManualResetEvent[] manualEvents = 
            new ManualResetEvent[numberOfFiles];
        State stateInfo;

        if(!Directory.Exists(dirName))
        {
            Directory.CreateDirectory(dirName);
        }

        // Queue the work items that create and write to the files.
        for(int i = 0; i < numberOfFiles; i++)
        {
            fileName = string.Concat(
                dirName, @"\Test", i.ToString(), ".dat");

            // Create random data to write to the file.
            byteArray = new byte[1000000];
            randomGenerator.NextBytes(byteArray);

            manualEvents[i] = new ManualResetEvent(false);

            stateInfo = 
                new State(fileName, byteArray, manualEvents[i]);

            ThreadPool.QueueUserWorkItem(new WaitCallback(
                Writer.WriteToFile), stateInfo);
        }
    
        // Since ThreadPool threads are background threads, 
        // wait for the work items to signal before exiting.
        WaitHandle.WaitAll(manualEvents);
        Console.WriteLine("Files written - main exiting.");
    }
}

// Maintain state to pass to WriteToFile.
class State
{
    public string fileName;
    public byte[] byteArray;
    public ManualResetEvent manualEvent;

    public State(string fileName, byte[] byteArray, 
        ManualResetEvent manualEvent)
    {
        this.fileName = fileName;
        this.byteArray = byteArray;
        this.manualEvent = manualEvent;
    }
}

class Writer
{
    static int workItemCount = 0;
    Writer() {}

    public static void WriteToFile(object state)
    {
        int workItemNumber = workItemCount;
        Interlocked.Increment(ref workItemCount);
        Console.WriteLine("Starting work item {0}.",
            workItemNumber.ToString());
        State stateInfo = (State)state;
        FileStream fileWriter = null;

        // Create and write to the file.
        try
        {
            fileWriter = new FileStream(
                stateInfo.fileName, FileMode.Create);
            fileWriter.Write(stateInfo.byteArray, 
                0, stateInfo.byteArray.Length);
        }
        finally
        {
            if(fileWriter != null)
            {
                fileWriter.Close();
            }

            // Signal Main that the work item has finished.
            Console.WriteLine("Ending work item {0}.", 
                workItemNumber.ToString());
            stateInfo.manualEvent.Set();
        }
    }
}
Imports System
Imports System.IO
Imports System.Security.Permissions
Imports System.Threading

Public Class Test

    ' WaitHandle.WaitAll requires a multithreaded apartment 
    ' when using multiple wait handles.
    <MTAThreadAttribute> _
    Shared Sub Main()
        Const numberOfFiles As Integer = 5
        Dim dirName As String = "C:\TestTest"
        Dim fileName As String 

        Dim byteArray() As Byte 
        Dim randomGenerator As New Random()

        Dim manualEvents(numberOfFiles - 1) As ManualResetEvent
        Dim stateInfo As State 

        If Directory.Exists(dirName) <> True Then
            Directory.CreateDirectory(dirName)
        End If

        ' Queue the work items that create and write to the files.
        For i As Integer = 0 To numberOfFiles - 1
            fileName = String.Concat( _
                dirName, "\Test", i.ToString(), ".dat")

            ' Create random data to write to the file.
            byteArray = New Byte(1000000){}
            randomGenerator.NextBytes(byteArray)

            manualEvents(i) = New ManualResetEvent(false)

            stateInfo = _ 
                New State(fileName, byteArray, manualEvents(i))

            ThreadPool.QueueUserWorkItem(AddressOf _
                Writer.WriteToFile, stateInfo)
        Next i
    
        ' Since ThreadPool threads are background threads, 
        ' wait for the work items to signal before exiting.
        WaitHandle.WaitAll(manualEvents)
        Console.WriteLine("Files written - main exiting.")
    End Sub

End Class
 
' Maintain state to pass to WriteToFile.
Public Class State

    Public fileName As String
    Public byteArray As Byte()
    Public manualEvent As ManualResetEvent

    Sub New(fileName As String, byteArray() As Byte, _
        manualEvent As ManualResetEvent)
    
        Me.fileName = fileName
        Me.byteArray = byteArray
        Me.manualEvent = manualEvent
    End Sub

End Class

Public Class Writer

    Private Sub New()
    End Sub

    Shared workItemCount As Integer = 0

    Shared Sub WriteToFile(state As Object)
        Dim workItemNumber As Integer = workItemCount
        Interlocked.Increment(workItemCount)
        Console.WriteLine("Starting work item {0}.", _
            workItemNumber.ToString())
        Dim stateInfo As State = CType(state, State)
        Dim fileWriter As FileStream = Nothing

        ' Create and write to the file.
        Try
            fileWriter = New FileStream( _
                stateInfo.fileName, FileMode.Create)
            fileWriter.Write(stateInfo.byteArray, _
                0, stateInfo.byteArray.Length)
        Finally
            If Not fileWriter Is Nothing Then
                fileWriter.Close()
            End If

            ' Signal Main that the work item has finished.
            Console.WriteLine("Ending work item {0}.", _
                workItemNumber.ToString())
            stateInfo.manualEvent.Set()
        End Try
    End Sub

End Class

Comentarios

AbandonedMutexException Novedades de la versión 2.0 de .NET Framework.AbandonedMutexException is new in the .NET Framework version 2.0. En versiones anteriores, el WaitAll devuelve del método true cuando se abandona una exclusión mutua.In previous versions, the WaitAll method returns true when a mutex is abandoned. A menudo, una exclusión mutua abandonada indica un error grave de codificación.An abandoned mutex often indicates a serious coding error. En el caso de una exclusión mutua todo el sistema, podría indicar que una aplicación ha finalizado abruptamente (por ejemplo, mediante el Administrador de tareas de Windows).In the case of a system-wide mutex, it might indicate that an application has been terminated abruptly (for example, by using Windows Task Manager). La excepción contiene información útil para la depuración.The exception contains information useful for debugging.

El WaitAll método devuelve cuando se señalizan todos los identificadores.The WaitAll method returns when all the handles are signaled. En algunas implementaciones, si se pasan más de 64 identificadores, un NotSupportedException se produce.On some implementations, if more than 64 handles are passed, a NotSupportedException is thrown. Si la matriz contiene duplicados, se produce un error en la llamada con un DuplicateWaitObjectException.If the array contains duplicates, the call fails with a DuplicateWaitObjectException.

Nota

El WaitAll método no se admite en subprocesos que tienen STAThreadAttribute.The WaitAll method is not supported on threads that have STAThreadAttribute.

Llamar a esta sobrecarga del método es equivalente a llamar a la WaitAll(WaitHandle[], Int32, Boolean) sobrecarga del método y especificar -1 (o Timeout.Infinite) para millisecondsTimeout y true para exitContext.Calling this method overload is equivalent to calling the WaitAll(WaitHandle[], Int32, Boolean) method overload and specifying -1 (or Timeout.Infinite) for millisecondsTimeout and true for exitContext.

Se aplica a