• <bdo id='fVcP1'></bdo><ul id='fVcP1'></ul>

  • <small id='fVcP1'></small><noframes id='fVcP1'>

    1. <tfoot id='fVcP1'></tfoot>

        <i id='fVcP1'><tr id='fVcP1'><dt id='fVcP1'><q id='fVcP1'><span id='fVcP1'><b id='fVcP1'><form id='fVcP1'><ins id='fVcP1'></ins><ul id='fVcP1'></ul><sub id='fVcP1'></sub></form><legend id='fVcP1'></legend><bdo id='fVcP1'><pre id='fVcP1'><center id='fVcP1'></center></pre></bdo></b><th id='fVcP1'></th></span></q></dt></tr></i><div id='fVcP1'><tfoot id='fVcP1'></tfoot><dl id='fVcP1'><fieldset id='fVcP1'></fieldset></dl></div>

        <legend id='fVcP1'><style id='fVcP1'><dir id='fVcP1'><q id='fVcP1'></q></dir></style></legend>
      1. C# - 在 PowerStatus 更改时引发事件

        时间:2023-09-16

            <tbody id='3EzHX'></tbody>
          • <bdo id='3EzHX'></bdo><ul id='3EzHX'></ul>
            <tfoot id='3EzHX'></tfoot>
            <i id='3EzHX'><tr id='3EzHX'><dt id='3EzHX'><q id='3EzHX'><span id='3EzHX'><b id='3EzHX'><form id='3EzHX'><ins id='3EzHX'></ins><ul id='3EzHX'></ul><sub id='3EzHX'></sub></form><legend id='3EzHX'></legend><bdo id='3EzHX'><pre id='3EzHX'><center id='3EzHX'></center></pre></bdo></b><th id='3EzHX'></th></span></q></dt></tr></i><div id='3EzHX'><tfoot id='3EzHX'></tfoot><dl id='3EzHX'><fieldset id='3EzHX'></fieldset></dl></div>

              • <small id='3EzHX'></small><noframes id='3EzHX'>

              • <legend id='3EzHX'><style id='3EzHX'><dir id='3EzHX'><q id='3EzHX'></q></dir></style></legend>
                  本文介绍了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 更改时引发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在动态事件处理程序中引用“this" 下一篇:控制台应用程序 C# 的关闭事件

                  相关文章

                  <tfoot id='eaeoX'></tfoot>
                1. <legend id='eaeoX'><style id='eaeoX'><dir id='eaeoX'><q id='eaeoX'></q></dir></style></legend>
                    <bdo id='eaeoX'></bdo><ul id='eaeoX'></ul>

                      <small id='eaeoX'></small><noframes id='eaeoX'>

                      <i id='eaeoX'><tr id='eaeoX'><dt id='eaeoX'><q id='eaeoX'><span id='eaeoX'><b id='eaeoX'><form id='eaeoX'><ins id='eaeoX'></ins><ul id='eaeoX'></ul><sub id='eaeoX'></sub></form><legend id='eaeoX'></legend><bdo id='eaeoX'><pre id='eaeoX'><center id='eaeoX'></center></pre></bdo></b><th id='eaeoX'></th></span></q></dt></tr></i><div id='eaeoX'><tfoot id='eaeoX'></tfoot><dl id='eaeoX'><fieldset id='eaeoX'></fieldset></dl></div>