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

Sunil A M 171 Reputation points
2023-04-25T17:42:42.72+00:00

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
C#
C#
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,648 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Jack J Jun 24,496 Reputation points Microsoft Vendor
    2023-04-26T07:08:02.5966667+00:00

    @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;
           
    
    
                Console.ReadKey();
            }
    
            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 );
                        break;
                    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);
                        break;
    
                }
                
         
            }
    

    Hope my code could help you.

    Best Regards,

    Jack

    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 83,206 Reputation points
    2023-08-01T12:37:19.4566667+00:00

    I get notifications with PowerRegisterSuspendResumeNotification on Windows 10 22H2 :

                DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS dnsp = new DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS();
                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);
                Marshal.FreeHGlobal(pDeviceNotify);
    
            private static uint DeviceNotifyCallbackRoutine(IntPtr Context, uint Type, IntPtr Setting)
            {
                Console.Beep(1000, 100);
                // PBT_APMSUSPEND, PBT_APMRESUMESUSPEND, PBT_APMRESUMEAUTOMATIC
                // 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);
    
            [StructLayout(LayoutKind.Sequential)]
            public struct DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS
            {
                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