CPU time event listener

This may be a dumb question, but do event listeners use processor cycles such as a timer, or are they inactive until the event fires?

Is it language specific or are all languages ​​processed basically the same?

I want to write a tiny service that does something only when a network disconnect event is disabled, and I do not want the service to use resources only for listening (except for memory, of course).

I plan to do something like this

using NetworkUtilities;
    ManagementEventWatcher networkAdapterArrivalWatcher = new ManagementEventWatcher("\\root\\wmi","SELECT * FROM MSNdis_NotifyAdapterArrival ");
                    networkAdapterArrivalWatcher.Options.Timeout = new TimeSpan(0,0,5);

    ManagementEventWatcher networkAdapterRemovalWatcher = new ManagementEventWatcher("\\root\\wmi","SELECT * FROM MSNdis_NotifyAdapterRemoval " );
                    networkAdapterRemovalWatcher.Options.Timeout = new TimeSpan(0,0,5);

    ConnectionNotifierHandler handler = new ConnectionNotifierHandler();
                    networkAdapterArrivalWatcher.EventArrived += new EventArrivedEventHandler(handler.Arrived);
                    networkAdapterRemovalWatcher.EventArrived += new EventArrivedEventHandler(handler.Removed);

    //Start watching for events
    networkAdapterArrivalWatcher.Start();
    networkAdapterRemovalWatcher.Start();

    public void Arrived(object sender, EventArrivedEventArgs e)
    {
            using (ManagementBaseObject ev = e.NewEvent)
            {
                //Log the event
            }
    }

    public void Removed(object sender, EventArrivedEventArgs e)
    {
            using (ManagementBaseObject ev = e.NewEvent)
            {
                //Log the event
            }           
    }
+3
source share
3 answers

# - ( ), " " - .

, , :

  • OnConnecitonPending .
  • OnWindowMessage .

, , , , .

+2

, ( ).

, .

+1

Typically, event listeners corresponding to the built-in event listener do not use processor time until the event is triggered. Rather, they are delegates that are triggered by the event. Their interest in the event is marked by the system. This is most obvious, for example, with Objective-C / Cocoa style delegates, but the principle applies just about everywhere.

+1
source

Source: https://habr.com/ru/post/1717295/


All Articles