Comment : enregistrer des événements pour les composants multithread
Mise à jour : novembre 2007
Lors de l'enregistrement d'événements pour les composants multithread, des considérations spéciales entrent en jeu. Vous devez fournir un moyen d'identifier le thread à partir duquel l'événement a eu lieu. Vous devez également vous assurer que les threads n'interfèrent pas entre eux lors de l'enregistrement d'événements. Pour plus d'informations, consultez Journaux des événements et composants multithread.
Pour utiliser les journaux des événements dans des applications multithread
Déclarez et créez le journal des événements. Pour plus d'informations, consultez Comment : créer et supprimer des journaux des événements personnalisés.
Affectez un identificateur unique à la propriété Name de chaque thread.
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;
Créez Source pour le journal des événements et affectez à Name une valeur de chaîne unique correspondant au thread. Pour plus d'informations sur la création d'un Source, consultez Comment : ajouter votre application en tant que source d'entrées d'un journal des événements.
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");
Affectez à la propriété Source du journal des événements le nom du thread, puis appelez la méthode WriteEntry.
Remarque : Assurez-vous d'obtenir un verrou exclusif sur le journal des événements avant de poursuivre ces opérations.
' 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"); }
Voir aussi
Tâches
Comment : créer et supprimer des journaux des événements personnalisés
Comment : écrire des entrées dans des journaux des événements
Comment : coordonner plusieurs threads d'exécution
Procédure pas à pas : création d'un composant simple multithread avec Visual Basic
Procédure pas à pas : création d'un composant simple multithread à l'aide de Visual C#