Program wykrywający zdarzenie systemowe zużywa 33% procesora

0

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();
        }
    }
}


8

W 31 linii.

1

i pewnie masz procek z 3 rdzeniami a te 33% to 100% jednego rdzenia...

0
while (true);

Ten wiersz zużywa 100% jednego procesora, daj Sleep wewnątrz pętli, wystarczy 20ms.

0

Dziękuję. Pomogło.

1 użytkowników online, w tym zalogowanych: 0, gości: 1