Reputation: 145
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...
Edit: New .cs file
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
{
}
}
Upvotes: 5
Views: 8720
Reputation: 11
You have mabye found what you looking for, but still.. This code works for me:
using System;
using Microsoft.Win32;
class Powerstatus
{
static void Main(string[] args)
{
SystemEvents.PowerModeChanged +=
new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged);
Console.ReadLine();
}
static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
Console.WriteLine(e.Mode);
}
}
Upvotes: 1
Reputation: 13976
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:
public static event PowerModeChangedEventHandler PowerModeChanged;
UPDATE:
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.
}
Upvotes: 4