我有兩個進程A和B.與Windows的多線程交互EventLog
進程A繼續每5秒寫入一次EventLogEntry。
進程B應該偵聽EventLog對象上的EntryWritten事件,並儘快在屏幕上報告已寫入條目。
如何創建應始終運行直到手動關閉的進程B.(exe)。
請參見下面的代碼片段:
class Observer
{
static void Main(string[] args)
{
EventLog log = new EventLog("logname", "MyMachine", "source");
log.EntryWritten += new EntryWrittenEventHandler(log_EntryWritten);
log.EnableRaisingEvents = true;
// The thread shouldn't exit here but wait till the event is received
// When received, should call the handler
// and then again keep waiting for next event.
}
static void log_EntryWritten(object sender, EntryWrittenEventArgs e)
{
if (e.Entry.Source == "source")
{
Console.WriteLine("Message " + e.Entry.Message);
Console.WriteLine("InstanceId " + e.Entry.InstanceId);
Console.WriteLine("Source " + e.Entry.Source);
Console.WriteLine("TimeWritten " + e.Entry.TimeWritten);
Console.ReadLine();
Console.WriteLine("\n");
}
}
}
如何能不能做到?
謝謝。