C# SystemEvents.PowerModeChanged fires too late
Asked Answered
U

0

6

I want to catch the PowerModeChanged event before the computer or notebook is suspended.

My code snippet is the following:

void SystemEvents_OnPowerChanged(object sender, PowerModeChangedEventArgs e)
    {
        string fileName = DateTime.Today.ToString("yyyy-MM-dd") + ".txt";   //Den aktuellen Tag ermittlen
        string time = DateTime.Now.ToString("HH:mm:ss");                    //Die Zeit beim Eintreten des Events ermitteln           

        switch (e.Mode)                                                     //Event abfangen
        {
            case PowerModes.Resume:                                         //Das Betriebssystem steht unmittelbar davor, aus dem Ruhezustand fortgesetzt zu werden, ...

                LogToFile(fileName, " - " + time + " -> Pause.");           //... sodass die Pause beendet ist.
                lblStatus.Text = "Arbeit, Arbeit! :)";
                break;

            case PowerModes.Suspend:                                        //Die Ausführung des Betriebssystems wird unterbrochen, ...

                LogToFile(fileName, "\n" + time);                           //... sodass die Pause beginnt, also wird ein Zeilenumbruch hinzugefügt.
                lblStatus.Text = "Luft filtern. :|";
                break;
        }
    }

If the standby is finished the event is fired two times. First e.Mode is equal to Suspend and at the second time e.Mode is equal Resume.

How can I pause the Standby-Mode and save the string to the file? Why is the event fired twice after the Standby, although it should fired once before and once after?

Unreel answered 3/10, 2015 at 15:33 Comment(2)
did nobody have an idea since nearly two years?Unreel
I don't know if you're still looking for answers, but I was able to find a few related questions. See here and here.Anemia

© 2022 - 2024 — McMap. All rights reserved.