Como: Eventos de log para componentes multithread

Quando o log de eventos de componentes multissegmentados, considerações especiais entram em cena. Você deve fornecer um meio de identificar o thread a partir do qual o evento veio. Você também deve garantir que os threads não interferem entre si quando o log de eventos. Para obter detalhes, consulte:Logs de eventos e componentes multithread.

Para usar os logs de eventos em aplicativos multithread

  1. Declarar e criar o log de eventos. Para obter detalhes, consulte:How to: Create and Remove Custom Event Logs.

  2. Definir o Name a propriedade de cada segmento a um identificador exclusivo.

    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. Criar uma nova Source para o log de eventos e o conjunto de Name para um valor de seqüência exclusiva que corresponde ao segmento. Para obter detalhes sobre a criação de um Source, consulte 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. Definir o Source propriedade de log de eventos para o nome do thread e chamada a WriteEntry método.

    ObservaçãoObservação

    Certifique-se de obter um bloqueio exclusivo no log de eventos antes de prosseguir com estas operações.

    ' 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");
    }
    

Consulte também

Tarefas

How to: Create and Remove Custom Event Logs

How to: Write Entries to Event Logs

Como: Coordenar vários Threads de execução

Demonstra Passo a passo: Criação de um componente Multithreaded simples com Visual Basic

Demonstra Passo a passo: Criação de um componente Multithreaded simples com Visual C#

Referência

Instrução SyncLock

Outros recursos

Multithreading em componentes