Cómo: Registrar eventos de componentes multiproceso

Cuando se registren eventos de componentes multiproceso, se deben tener en cuenta consideraciones especiales. Se deben suministrar los medios para identificar el subproceso del que proviene el evento y, además, se debe garantizar que los subprocesos no interfieran unos con otros cuando se registran los eventos. Para obtener información detallada, vea Registros de eventos y componentes multiproceso.

Para usar registros de eventos en aplicaciones multiproceso

  1. Declare y cree el registro de eventos. Para obtener información detallada, vea How to: Create and Remove Custom Event Logs.

  2. Establezca la propiedad Name de cada subproceso un identificador único.

    Dim X as Integer
    X = 0
    Dim MyThread as New Threading.Thread(AddressOf MyMethod)
    MyThread.Name = "Thread number " & X.ToString
    ' Increments X so no other threads will share the same name.
    X += 1
    
    int X;
    X = 0;
    Thread MyThread = new Thread(new 
       ThreadStart(MyMethod));
    MyThread.Name = "Thread Number " + X.ToString();
    // Increments X so no other threads will share the same name.
    X += 1;
    
  3. Cree un nuevo objeto Source para el registro de eventos y establezca la propiedad Name como un valor de cadena único que se corresponda con el subproceso. Para obtener detalles sobre cómo crear Source, vea How to: Add Your Application as a Source of Event Log Entries.

    Imports System.Threading
    ' Checks to see if there already is a source with the name of the 
    ' thread.
    If Not EventLog.SourceExists(Thread.CurrentThread.Name.ToString, _
       "myApplication") Then
       ' Creates a source with the name of the thread
       EventLog.CreateEventSource(Thread.CurrentThread.Name.ToString, _
          "MyApplication")
    End If
    
    // These lines go at the top of the code
    using System.Threading;
    using System.Diagnostics;
    // Checks to see if there already is a source with the name of the 
    // thread.
    if (!EventLog.SourceExists(CurrentThread.Name.ToString()))
       // Creates a source with the name of the thread.
       EventLog.CreateEventSource(CurrentThread.Name.ToString(), 
          "MyApplication");
    
  4. Establezca la propiedad Source del registro de eventos como el nombre del subproceso y llame al método WriteEntry.

    Nota

    Asegúrese de obtener un bloqueo único sobre el registro de eventos antes de realizar estas operaciones.

    ' Use the SyncLock keyword to obtain an exclusive lock on an object.
    SyncLock MyEventLog
      MyEventLog.Source = Thread.CurrentThread.Name.ToString
      EventLog.WriteEntry(Thread.CurrentThread.Name.ToString, _
         "Error in Widget 42")
    End SyncLock
    
    // Use the lock keyword to obtain an exclusive lock on an object
    lock(MyEventLog)
    {
      MyEventLog.Source = Thread.CurrentThread.Name.ToString();
      EventLog.WriteEntry(Thread.CurrentThread.Name.ToString(),
         "Error in Widget 42");
    }
    

Vea también

Tareas

Cómo: Coordinar varios subprocesos de ejecución

Tutorial: Crear un componente sencillo con múltiples procesos en Visual Basic

Tutorial: Crear un componente sencillo con múltiples procesos en Visual C#

Referencia

SyncLock (Instrucción)

Otros recursos

How to: Create and Remove Custom Event Logs

How to: Write Entries to Event Logs

Subprocesamiento múltiple en componentes