C# - 在 PowerStatus 更改时引发事件 [英] C# - Raise event on PowerStatus change

查看:26
本文介绍了C# - 在 PowerStatus 更改时引发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个需要处于安全状态的应用程序,因此我想在后台跟踪计算机的电源状态.如果电池电量(如果有)低或严重,我将不允许用户继续使用应用程序并正常退出.

I've created an application that need to be in a safe state and so I want to follow the power status of the computer in background. If the battery level (if any) is low or critical, I wouldn't allow the user to continue using the application and quit all properly.

首先,我很惊讶不存在这样的事件来检测变化.您需要始终手动检查 PowerStatus.

First of all, I'm astonished that no such event exists in order to detect the change. You need always to check the PowerStatus manually.

所以,我围绕它创建了一个包装器,如下所示:

So, I've created a wrapper around it, something like this :

using System;
using System.Windows.Forms;

namespace MyApp
{
    internal static class BatteryManagement
    {
        //
        internal static event EventHandler<EventArgs> Changed;

        //
        private static bool _started;
        private static System.Threading.Timer _timer;
        private static PowerStatus _previousPowerStatus;

        internal static void Start()
        {
            if (!_started)
            {
                _started = true;
                ManageBatteryLevel();
            }
        }

        internal static void Stop()
        {
            if (_started)
            {
                if(_timer != null)
                {
                    _timer.Dispose();
                    _timer = null;
                }

                _started = false;
            }
        }

        private static void ManageBatteryLevel()
        {
            _previousPowerStatus = new PowerStatus();

            TimeSpan dueTime = new TimeSpan(0, 0, 0); // Start right now
            TimeSpan period = new TimeSpan(0, 1, 0); // Run every 1 minute

            // Setting a timer that launch every period the OnBatteryLevelChange method
            _timer = new System.Threading.Timer(OnBatteryLevelChange, null, dueTime, period);
        }

        private static void OnBatteryLevelChange(Object stateInfo)
        {
            PowerStatus powerStatus = new PowerStatus();

            if (!_previousPowerStatus.Equals(powerStatus))
            {
                // Ensure battery level is up to date before raising event
                _previousPowerStatus = powerStatus;

                if (Changed != null)
                {
                    Changed(null, EventArgs.Empty);
                }
            }
        }
    }
}

但不起作用,因为 PowerStatus 没有任何公共构造函数,我无法存储先前状态的结果...

But doesn't work because PowerStatus hasn't any public constructor and I can't store the result of the previous status...

我该如何管理?

谢谢...

推荐答案

其实有,它叫SystemEvents.PowerModeChanged

如果PowerModeChangedEventArgsModeStatusChange,则表示电池状态发生了变化.

If the PowerModeChangedEventArgs has a Mode of StatusChange, it means the status of the battery has changed.

static void SystemEvents_PowerModeChanged(object sender, Microsoft.Win32.PowerModeChangedEventArgs e)
{
    if (e.Mode == Microsoft.Win32.PowerModes.StatusChange)
    {
         // Check what the status is and act accordingly
    }
}

本教程也可能有用:

http://netcode.ru/dotnet/?lang=&katID=30&skatID=277&artID=7643

这篇关于C# - 在 PowerStatus 更改时引发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆