Napisałem poniższy program do monitorowania zajścia zdarzenia o identyfikatorze z 10000 z dziennika aplikacji i usług "Microsoft-Windows-NetworkProfile/Działa". Program reaguje na zdarzenie, ale w aplikacji "Menedżer zadań" jest wykazane, że zużywa około 33% procesora. Gdzie tkwi problem?
using System;
using System.Diagnostics.Eventing.Reader;
namespace ConsoleApp2
{
public class SubscribeToEventsExample
{
public SubscribeToEventsExample()
{
EventLogWatcher watcher;
watcher = null;
try
{
// Subscribe to receive event notifications
// in the Application log. The query specifies
// that only level 2 events will be returned.
EventLogQuery subscriptionQuery = new EventLogQuery("Microsoft-Windows-NetworkProfile/Operational", PathType.LogName, "*[System/EventID=10000]");
watcher = new EventLogWatcher(subscriptionQuery);
// Set watcher to listen for the EventRecordWritten
// event. When this event happens, the callback method
// (EventLogEventRead) will be called.
watcher.EventRecordWritten += new EventHandler<EventRecordWrittenEventArgs>(HandleEvent);
// Begin subscribing to events the events
watcher.Enabled = true;
Console.WriteLine("Waiting for events...");
while (true);
}
catch (EventLogReadingException e)
{
Console.WriteLine("Error reading the log: {0}", e.Message);
}
finally
{
// Stop listening to events
watcher.Enabled = false;
if (!(watcher is null))
{
watcher.Dispose();
}
}
}
public static void HandleEvent(object obj, EventRecordWrittenEventArgs arg)
{
if (!(arg.EventRecord is null))
{
Console.WriteLine("Received event {0} from the subscription.", arg.EventRecord.Id);
Console.WriteLine("Description: {0}", arg.EventRecord.FormatDescription());
}
else
{
Console.WriteLine("The event instance was null.");
}
}
static void Main(string[] args)
{
SubscribeToEventsExample eventWatcher = new SubscribeToEventsExample();
}
}
}