protected void OnEventReceived(PowerEventArgs e)
 {
     if (this.EventReceived != null)
     {
         EventReceived(this, e);
     }
 }
        void window_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            if (e.Message.Msg == WM_POWERBROADCAST)
            {
                var args = new PowerEventArgs()
                {
                    EventType = e.Message.WParam.ToInt32(),
                    Data      = e.Message.LParam
                };

                OnEventReceived(args);
            }
        }
Ejemplo n.º 3
0
        protected int HandlerEx(int control, int eventType, IntPtr eventData, IntPtr context)
        {
            switch (control)
            {
            case NativeMethods.SERVICE_CONTROL_STOP:
                OnServiceStop();
                break;

            case NativeMethods.SERVICE_CONTROL_PAUSE:
                OnServicePause();
                break;

            case NativeMethods.SERVICE_CONTROL_CONTINUE:
                OnServiceContinue();
                break;

            case NativeMethods.SERVICE_CONTROL_INTERROGATE:
                return(NativeMethods.NO_ERROR);

            case NativeMethods.SERVICE_CONTROL_SHUTDOWN:
                OnServiceShutdown();
                break;

            case NativeMethods.SERVICE_CONTROL_POWEREVENT:
                var args = new PowerEventArgs()
                {
                    EventType = eventType,
                    Data      = eventData
                };

                OnEventReceived(args);
                break;
            }

            return(NativeMethods.NO_ERROR);
        }
Ejemplo n.º 4
0
        void provider_EventReceived(object sender, PowerEventArgs e)
        {
            if (e.EventType == NativeMethods.PBT_POWERSETTINGCHANGE)
            {
                var powerSettings = (NativeMethods.POWERBROADCAST_SETTING)Marshal.PtrToStructure(
                    e.Data, typeof(NativeMethods.POWERBROADCAST_SETTING));

                if (powerSettings.PowerSetting == NativeMethods.GUID_ACDC_POWER_SOURCE)
                {
                    var args = new PowerLineStatusEventArgs()
                    {
                        PowerCondition = (SystemPowerCondition)powerSettings.Data
                    };

                    OnPowerLineStatusChanged(args);
                }
                else if (powerSettings.PowerSetting == NativeMethods.GUID_BATTERY_PERCENTAGE_REMAINING)
                {
                    var args = new BatteryStatusEventArgs()
                    {
                        BatteryPercentageRemaining = (int)powerSettings.Data
                    };

                    OnBatteryStatusChanged(args);
                }
                else if (powerSettings.PowerSetting == NativeMethods.GUID_LIDSWITCH_STATE_CHANGE)
                {
                    var args = new LidswitchStateEventArgs()
                    {
                        IsLidOpen = powerSettings.Data != 0
                    };

                    OnLidswitchStateChanged(args);
                }
                else if (powerSettings.PowerSetting == NativeMethods.GUID_POWERSCHEME_PERSONALITY)
                {
                    var args = new PowerSchemeEventArgs();

                    if (powerSettings.PowerSchemePersonality == NativeMethods.GUID_TYPICAL_POWER_SAVINGS)
                    {
                        args.Personality = PowerSchemePersonality.Automatic;
                    }
                    else if (powerSettings.PowerSchemePersonality == NativeMethods.GUID_MIN_POWER_SAVINGS)
                    {
                        args.Personality = PowerSchemePersonality.PowerSaver;
                    }
                    else if (powerSettings.PowerSchemePersonality == NativeMethods.GUID_MAX_POWER_SAVINGS)
                    {
                        args.Personality = PowerSchemePersonality.HighPerformance;
                    }

                    OnPowerSchemePersonalityChanged(args);
                }
                else if (powerSettings.PowerSetting == NativeMethods.GUID_MONITOR_POWER_ON ||
                         powerSettings.PowerSetting == NativeMethods.GUID_CONSOLE_DISPLAY_STATE)
                {
                    var args = new DisplayStateEventArgs()
                    {
                        DisplayState = (DisplayState)powerSettings.Data
                    };

                    OnDisplayStateChanged(args);
                }
            }
        }