使用下面的代碼片段,我試圖做「預訂Windows事件日誌」。有辦法刪除線程休眠代碼嗎?
using System;
using System.Diagnostics.Eventing.Reader;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
EventLogWatcher watcher = null;
try
{
EventLogQuery subscriptionQuery = new EventLogQuery("Application", PathType.LogName, "*[System/EventID=101]");
watcher = new EventLogWatcher(subscriptionQuery);
watcher.EventRecordWritten += new EventHandler<EventRecordWrittenEventArgs>(EventLogEventRead);
// Activate the subscription
watcher.Enabled = true;
for (int i = 0; i < 5; i++)
{
// Wait for events to occur.
System.Threading.Thread.Sleep(10000);
}
}
catch (EventLogReadingException e)
{
Console.WriteLine(e.Message);
}
finally
{
// Stop listening to events
watcher.Enabled = false;
if (watcher != null)
{
watcher.Dispose();
}
}
Console.ReadKey();
}
private static void EventLogEventRead(object obj, EventRecordWrittenEventArgs arg)
{
// Make sure there was no error reading the event.
if (arg.EventRecord != null)
{
Console.WriteLine(arg.EventRecord.TimeCreated);
Console.WriteLine(arg.EventRecord.FormatDescription());
}
else
{
Console.WriteLine("The event instance was null.");
}
}
}
}
在這裏一切看起來不錯,但有什麼辦法可以刪除線程睡眠代碼?
for (int i = 0; i < 5; i++)
{
// Wait for events to occur.
System.Threading.Thread.Sleep(10000);
}
什麼可能是其他解決方案,可以調用事件發生?
是:用鼠標點擊「for」以將光標置於其上,然後按住「刪除」鍵直到有問題的代碼段被刪除。爲了得到答案,你需要解釋你需要完成什麼,以及爲什麼你顯示的代碼沒有完成。 –
你想等待用戶的停止請求嗎? –
你想繼續等待50秒嗎?或者是否有一些事件發生? – Enigmativity