event to log the text while system goes to sleep and resume back.

Sunil A M 171 Reputation points

Hi Team, i need some help for the below requirement.

  1. I need to write a log(to text file) from C# class library when the system goes to sleep.
  2. I need to write a log(to text file) from C# class library when the system resume back from sleep - this is important for me.
  3. I need to write a log(to text file) from C# class library when the system resume back from hibernate. Could you please help me with some sample. Best Regards, Sunil
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,459 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Jack J Jun 24,476 Reputation points Microsoft Vendor

    @Sunil A M, Welcome to Microsoft Q&A, I recommend that you use SystemEvents.PowerModeChanged event to detect if system is going to sleep or resume back.

    Please note that there is no direct way to tell the difference between system resume back from sleep and system resume back from hibernate.

    You could look at the answer to know more about the reason. Therefore, you could try the following code to do something when the system goes to sleep or when the system resume back from sleep or hibernate.

            static void Main(string[] args)
                SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
            private static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
                switch (e.Mode)   
                    case PowerModes.Resume:
                        var now = DateTime.Now;
                        // Build the log message
                        var message = $"System resumed from sleep at {now.ToString()}";
                        // Write the message to a text file
                        var logPath = "C:\\Logs\\";
                        var fileName = "resumelog.txt";
                        var fullPath = Path.Combine(logPath, fileName);
                        File.WriteAllText(fullPath, message );
                    case PowerModes.Suspend:
                        var now1 = DateTime.Now;
                        // Build the log message
                        var message1 = $"System went to sleep at {now1.ToString()}";
                        // Write the message to a text file
                        var logPath1 = "C:\\Logs\\";
                        var fileName1 = "sleeplog.txt";
                        var fullPath1 = Path.Combine(logPath1, fileName1);
                        File.WriteAllText(fullPath1, message1);

    Hope my code could help you.

    Best Regards,


    If the answer is helpful, please click "Accept Answer" and upvote it. Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.  

  2. Castorix31 82,241 Reputation points

    I get notifications with PowerRegisterSuspendResumeNotification on Windows 10 22H2 :

                dnsp.Callback = new DEVICE_NOTIFY_CALLBACK_ROUTINE(DeviceNotifyCallbackRoutine);
                IntPtr pDeviceNotify = Marshal.AllocHGlobal(Marshal.SizeOf(dnsp));
                Marshal.StructureToPtr(dnsp, pDeviceNotify, false);
                IntPtr pRegistrationHandle = IntPtr.Zero;
                uint nRet = PowerRegisterSuspendResumeNotification(DEVICE_NOTIFY_CALLBACK, pDeviceNotify, out pRegistrationHandle);
            private static uint DeviceNotifyCallbackRoutine(IntPtr Context, uint Type, IntPtr Setting)
                Console.Beep(1000, 100);
                // System.Diagnostics.Debug.WriteLine(string.Format("DeviceNotify - Type = 0x{0:X4}", Type));
                System.Diagnostics.Debug.WriteLine(string.Format("DeviceNotify - Type = {0}", ((PBT)Type).ToString()));
                return 0;

    Declarations :

            [DllImport("Powrprof.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            public static extern uint PowerRegisterSuspendResumeNotification(uint Flags, IntPtr Recipient, out IntPtr RegistrationHandle);
                public DEVICE_NOTIFY_CALLBACK_ROUTINE Callback;
                public IntPtr Context;
            public delegate uint DEVICE_NOTIFY_CALLBACK_ROUTINE(IntPtr Context, uint Type, IntPtr Setting);
            public const int DEVICE_NOTIFY_CALLBACK = 2;
            public enum PBT : int
                PBT_APMQUERYSUSPEND = 0x0000,
                PBT_APMQUERYSTANDBY = 0x0001,
                PBT_APMSUSPEND = 0x0004,
                PBT_APMSTANDBY = 0x0005,
                PBT_APMRESUMECRITICAL = 0x0006,
                PBT_APMRESUMESUSPEND = 0x0007,
                PBT_APMRESUMESTANDBY = 0x0008,
                PBT_APMBATTERYLOW = 0x0009,
                PBT_APMPOWERSTATUSCHANGE = 0x000A,
                PBT_APMOEMEVENT = 0x000B,
                PBT_APMRESUMEAUTOMATIC = 0x0012,
                PBT_POWERSETTINGCHANGE = 0x8013,
    0 comments No comments