C#Win32睡眠检测PowerModeChanged [英] C# Win32 Sleep Detection PowerModeChanged

查看:226
本文介绍了C#Win32睡眠检测PowerModeChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在网上搜索了大约一个小时,并在stackoverflow和其他站点上尝试了所有不同的策略,但是由于某种原因,我无法使它正常工作.我希望我的程序在计算机进入睡眠状态时将其写入控制台,但无法这样做.我认为我将其设置为错误.这就是我所拥有的...

I have been searching the web for about an hour and have tried all the different tactics on both stackoverflow and others sites but for some reason I am unable to get this to work. I want me program to write to the console when the computer it going to sleep but am unable to do so. I think I am setting this up wrong. Here is what I have...

新的.cs文件

using LightFX;
using System;
using System.Threading;
using System.Text;
using Microsoft.Win32;
using ConsoleApplication3;

namespace SampleApp5
{
    class Program
    {
        static void Main()
        {

        }

        SystemEvents.PowerModeChanged += PowerModeChanged;
    }
    class PowerModeChanged
    {

    }
}

推荐答案

该事件由

That event is raised by the SystemEvents class (it's a static event). You just need to bind to it:

SystemEvents.PowerModeChanged += OnPowerModeChanged;

您需要在主方法中的某个位置执行此操作.

You need to do this somewhere in your main method.

并且,当然,请删除您的自定义委托,因为它已经由框架定义.您不需要此行:

And, of course, delete your custom delegate, since it's already defined by the framework. You don't need this line:

公共静态事件PowerModeChangedEventHandler PowerModeChanged;

public static event PowerModeChangedEventHandler PowerModeChanged;

更新:

    static void Main()
    {
       SystemEvents.PowerModeChanged += OnPowerModeChanged;
       Console.ReadLine(); //just wait, don't exit immediately.
    }

    private static void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e) 
    {
        //Handle the event here.
    }

这篇关于C#Win32睡眠检测PowerModeChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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