Compartilhar via


Como: Eventos de log para componentes multithread

Ao registrar eventos de componentes multissegmentados, considerações especiais entram em cena.Você deve fornecer um meio de identificar o thread de onde veio o evento.Você também deve garantir que os threads não interferem uma da outra quando o registrar registrar registrar em log de eventos.Para obter detalhes, consulte:Componentes multithread e evento logs.

Para usar evento registra em aplicativos multithread

  1. Declarar e criar o log de eventos.Para obter detalhes, consulte:Como: Criar e Remover Personalizar Event Logs.

  2. conjunto o Name propriedade de cada thread 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 um novo Source para o log de eventos e defina o Name como um valor seqüência exclusiva que corresponde ao segmento. Para obter detalhes sobre como criar um Source, consulte Como: Adicionar O aplicativo como uma fonte de entradas de log de eventos.

    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. conjunto o Source propriedade do evento log para o nome do thread e telefonar a WriteEntry método.

    Observação:

    Certifique-se de obter um bloquear 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

Como: Criar e Remover Personalizar Event Logs

Como: Gravar entradas de log de eventos

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

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

Demonstra Passo a passo: Criação de um componente Multithreaded simples com translation from VPE for Csharp Visual

Referência

Instrução SyncLock

Outros recursos

Multithreading in Components