none
Resume event not firing after resuming from sleep RRS feed

  • Question

  • Hello,

    I am trying to develop a small app in c# 4.0 .net framework to help me start/stop an application when going/resuming from sleep. I am using the following code


    SystemEvents.PowerModeChanged += OnPowerChange;


    private void OnPowerChange(Object sender, PowerModeChangedEventArgs e) { string lines; try { switch (e.Mode) { /// When resume has occured case PowerModes.Resume: lines = "Resuming.\r\n"; System.IO.StreamWriter file = new System.IO.StreamWriter("out1.txt"); file.WriteLine(lines); file.Close(); break; /// When Suspend has occured case PowerModes.Suspend: lines = "Suspending.\r\n"; file = new System.IO.StreamWriter("out2.txt"); file.WriteLine(lines); file.Close(); break; case PowerModes.StatusChange: lines = "Status Changed.\r\n"; file = new System.IO.StreamWriter("out3.txt"); file.WriteLine(lines); file.Close(); break; default: break; } } catch (Exception ex) { lines = "Error: \r\n" + ex.Message + "\r\n"; System.IO.StreamWriter file = new System.IO.StreamWriter("error.txt"); file.WriteLine(lines); file.Close(); } }

    The problem is that on my developing machine everything works fine but when I tried it on my home theater pc when resuming from sleep nothing happens. The only time the above event fires is when I enter suspend mode. When I resume from sleep nothing happens.

    The only difference that exist between the two machines and can potentially be causing the issue is that in my htpc I pass the video through a yamaha receiver and from there to my TV. I notice that every time the problem appears nothing is shown in my TV (no video). I have to press some key on my remote to get my TV to show something. On the development machine however everything is working fine and both of my monitors are displaying video after resuming from sleep.

    Does anybody has any idea why this is happening? Could this be a bug in .net framework that is created when you pass the hdmi video through a receiver before connecting it to a Monitor/TV?

    Regards

    phanos

    Wednesday, May 28, 2014 7:44 PM

All replies

  • Hello,

    The SystemEvents.PowerModeChanged event is only raised if the message pump is running. In a Windows service, unless a hidden form is used or the message pump has been started manually, this event will not be raised. You can have a try to create a hidden forum and start the message pump manually as below:

    namespace SimpleServiceCs
    
    {
    
        public class SimpleService : ServiceBase
    
        {
    
            static void Main(string[] args)
    
            {
    
                ServiceBase.Run(new SimpleService());
    
            }
    
    
            protected override void OnStart(string[] args)
    
            {
    
                EventLog.WriteEntry("SimpleService", "Starting SimpleService");
    
                new Thread(RunMessagePump).Start();
    
            }
    
    
            void RunMessagePump()
    
            {
    
                EventLog.WriteEntry("SimpleService.MessagePump", "Starting SimpleService Message Pump");
    
                Application.Run(new HiddenForm());
    
            }
    
    
            protected override void OnStop()
    
            {
    
                Application.Exit();
    
            }
    
        }
    
    
        public partial class HiddenForm : Form
    
        {
    
            public HiddenForm()
    
            {
    
                InitializeComponent();
    
            }
    
    
            private void HiddenForm_Load(object sender, EventArgs e)
    
            {
    
                SystemEvents.TimeChanged += new EventHandler(SystemEvents_TimeChanged);
    
                SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(SystemEvents_UPCChanged);
    
            }
    
    
            private void HiddenForm_FormClosing(object sender, FormClosingEventArgs e)
    
            {
    
                SystemEvents.TimeChanged -= new EventHandler(SystemEvents_TimeChanged);
    
                SystemEvents.UserPreferenceChanged -= new UserPreferenceChangedEventHandler(SystemEvents_UPCChanged);
    
            }
    
    
            private void SystemEvents_TimeChanged(object sender, EventArgs e)
    
            {
    
                EventLog.WriteEntry("SimpleService.TimeChanged", "Time changed; it is now " +
    
                    DateTime.Now.ToLongTimeString());
    
            }
    
    
            private void SystemEvents_UPCChanged(object sender, UserPreferenceChangedEventArgs e)
    
            {
    
                EventLog.WriteEntry("SimpleService.UserPreferenceChanged", e.Category.ToString());
    
            }
    
        }
    
    
        partial class HiddenForm
    
        {
    
            private System.ComponentModel.IContainer components = null;
    
    
            protected override void Dispose(bool disposing)
    
            {
    
                if (disposing && (components != null))
    
                {
    
                    components.Dispose();
    
                }
    
                base.Dispose(disposing);
    
            }
    
    
            private void InitializeComponent()
    
            {
    
                this.SuspendLayout();
    
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    
                this.ClientSize = new System.Drawing.Size(0, 0);
    
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    
                this.Name = "HiddenForm";
    
                this.Text = "HiddenForm";
    
                this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
    
                this.Load += new System.EventHandler(this.HiddenForm_Load);
    
                this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.HiddenForm_FormClosing);
    
                this.ResumeLayout(false);
    
    
            }
    
        }
    
    
        [RunInstaller(true)]
    
        public class SimpleInstaller : Installer
    
        {
    
            private ServiceInstaller serviceInstaller;
    
            private ServiceProcessInstaller processInstaller;
    
    
            public SimpleInstaller()
    
            {
    
                processInstaller = new ServiceProcessInstaller();
    
                serviceInstaller = new ServiceInstaller();
    
    
                // Service will run under system account
    
                processInstaller.Account = ServiceAccount.LocalSystem;
    
    
                // Service will have Start Type of Manual
    
                serviceInstaller.StartType = ServiceStartMode.Automatic;
    
    
                serviceInstaller.ServiceName = "Simple Service";
    
    
                Installers.Add(serviceInstaller);
    
                Installers.Add(processInstaller);
    
            }
    
        }
    
    }
    

    For details, you can refer to this link:

    http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents(v=vs.110).aspx

    If this does not work for you, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, May 29, 2014 8:46 AM
    Moderator
  • Hi Fred and thanks for the reply. Actually my code is part of a visible windows form. I have created a simple form after trying to narrow down the problem. I am registering the event in the form constructor and then try to sleep my machine while connected to it through remote desktop. When try to resume the machine from sleep using wake timers or wake on lan the event is not being fired. When I try the power button the event is fired correctly. Does any of the above steps are wrong in order to have the event fired?  

    Thursday, May 29, 2014 9:31 AM
  • Hi phanospa,

    As far as I know, applications  as windows form will be stopped if the PC goes into sleep mode, so all its function will not be called unless the PC wakes up. I would like suggest you using the windows service application which can work even in sleep mode as the link I provided.

    If I misunderstand, please let me know.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, May 30, 2014 7:26 AM
    Moderator
  • Hi do you really think that moving my app from form to a service will help? I have tried many times on both the PCs  and the result was the same. If you really think it will help I will try to give it a shot!
    Friday, May 30, 2014 10:46 AM
  • Hello Fred. I went on and test your theory but unfortunately my problem is not solved. The service reacted the same way as my application does.

    Any more ideas?

    I have also created a bug report at microsoft. Maybe they can shed some light in the matter.

    Sunday, June 1, 2014 4:37 PM
  • Hi phanospa,

    Since i cannot reproduce this issue, i am trying to invoke someine experienced to look into it, as soon as we have any result, we will tell you.

    Thanks for your understanding.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 2, 2014 7:09 AM
    Moderator
  • Thanks Fred
    Monday, June 2, 2014 5:56 PM
  • Hello Fred,

    just another note that might help. I notice that on the problematic machine when the problem appears wake timers are also disabled. In order to enable wake timers again I have to reboot the machine or sleep the machine and then resume using the power button.

    Hope this help to narrow down the issue.

    Wednesday, July 2, 2014 4:33 PM