locked
SystemEvents not working from a Windows Service RRS feed

  • Question

  • Requirements: Windows 10 1909 Build 18363, Visual Studio 2019 Version 16.6.1

    The MSFT documentation on SystemEvents gives a clear example of how to hook SystemEvents into a Windows Service, by creating a hidden Form.  However, following the instructions exactly, I am not able to get any of the SystemEvents to log a message to the Event Viewer.  Does this no longer work?  I am running Windows 10 1909 (Build 18363), using Visual Studio 19 Version 16.6.1.

    Program.cs:

    using System.ServiceProcess;
    
    namespace WindowsServiceWithGuiTest
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            static void Main()
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[]
                {
                    new Service1()
                };
                ServiceBase.Run(ServicesToRun);
            }
        }
    }
    

    ProjectInstaller.cs:

    using System.ComponentModel;
    
    namespace WindowsServiceWithGuiTest
    {
        [RunInstaller(true)]
        public partial class ProjectInstaller : System.Configuration.Install.Installer
        {
            public ProjectInstaller()
            {
                InitializeComponent();
            }
        }
    }
    

    ProjectInstaller.Designer.cs 'InitializeComponent' method:

            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
                this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
                // 
                // serviceProcessInstaller1
                // 
                this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
                this.serviceProcessInstaller1.Password = null;
                this.serviceProcessInstaller1.Username = null;
                // 
                // serviceInstaller1
                // 
                this.serviceInstaller1.Description = "This is a test service that also launches a GUI which you can hook system events " +
        "into.";
                this.serviceInstaller1.DisplayName = "ServiceWithGuiTest";
                this.serviceInstaller1.ServiceName = "ServiceWithGuiTest";
                this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
                // 
                // ProjectInstaller
                // 
                this.Installers.AddRange(new System.Configuration.Install.Installer[] {
                this.serviceProcessInstaller1,
                this.serviceInstaller1});
    
            }
    

    Service1.cs:

    using Microsoft.Win32;
    using System;
    using System.Diagnostics;
    using System.ServiceProcess;
    using System.Threading;
    using System.Windows.Forms;
    
    namespace WindowsServiceWithGuiTest
    {
        public partial class Service1 : ServiceBase
        {
            public Service1()
            {
                InitializeComponent();
            }
    
            protected override void OnStart(string[] args)
            {
                EventLog.WriteEntry("WindowsServiceWithGuiTest", "Starting Service");
                new Thread(RunMessagePump).Start();
            }
    
            void RunMessagePump()
            {
                EventLog.WriteEntry("WindowsServiceWithGuiTest.MessagePump", "Starting Message Pump");
                Application.Run(new HiddenForm());
            }
    
            protected override void OnStop()
            {
                Application.Exit();
            }
    
            public partial class HiddenForm : Form
            {
                public HiddenForm()
                {
                    InitializeComponent();
                    EventLog.WriteEntry("WindowsServiceWithGuiTest.HiddenForm_FormClosing", "Form c'tor finished");
                }
    
                private void HiddenForm_Load(object sender, EventArgs e)
                {
                    EventLog.WriteEntry("WindowsServiceWithGuiTest.DisplaySettingsChanged", "Form loaded");
                    SystemEvents.TimeChanged += new EventHandler(SystemEvents_TimeChanged);
                    SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(SystemEvents_UPCChanged);
                    SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged;
                }
    
                private void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
                {
                    EventLog.WriteEntry("WindowsServiceWithGuiTest.DisplaySettingsChanged", "Display Settings Have changed");
                }
    
                private void HiddenForm_FormClosing(object sender, FormClosingEventArgs e)
                {
                    EventLog.WriteEntry("WindowsServiceWithGuiTest.HiddenForm_FormClosing", "Form closing");
                    SystemEvents.TimeChanged -= new EventHandler(SystemEvents_TimeChanged);
                    SystemEvents.UserPreferenceChanged -= new UserPreferenceChangedEventHandler(SystemEvents_UPCChanged);
                    SystemEvents.DisplaySettingsChanged -= SystemEvents_DisplaySettingsChanged;
                }
    
                private void SystemEvents_TimeChanged(object sender, EventArgs e)
                {
                    EventLog.WriteEntry("WindowsServiceWithGuiTest.TimeChanged", "Time changed; it is now " +
                        DateTime.Now.ToLongTimeString());
                }
    
                private void SystemEvents_UPCChanged(object sender, UserPreferenceChangedEventArgs e)
                {
                    EventLog.WriteEntry("WindowsServiceWithGuiTest.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);
                }
            }
        }
    }
    

    When I compile, install, and start the service, then try to do something like change the time, change my desktop background, or change my display resolution, NOTHING HAPPENS!  The last seen event log message is that the "Form loaded".

    Any help would be greatly appreciated!

    Tam

    Wednesday, June 10, 2020 10:00 PM

Answers

  • Hi Tam Bui,

    Thanks for your feedback.

    >>I got the TimeChanged System Event to log something.  But it still didn't work for UserPreferenceChanged (which the documentation claims) or DisplaySettingsChanged (which is what I really want).

    I make a test again and reproduce your problem: It didn't work for UserPreferenceChanged and DisplaySettingsChanged.

    In order to get more help about this problem, you can consider reporting your problem in developer community or give your feedback in MicrosoftDocs.

    Thank you for your understanding.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Tam Bui - Dev Friday, June 12, 2020 3:59 PM
    Friday, June 12, 2020 8:15 AM

All replies

  • To make sure that the handlers are installed, write a log message at the end of HiddenForm_Load. Or add try-catch for further investigations.

    Thursday, June 11, 2020 3:22 AM
  • Hi Tam Bui,

    Thank you for posting here.

    I make a test based on the SystemEvents document, and successfully run the service.

    You problem may related to your form.

    I add the 'HiddenForm' by right clicking the 'SimpleService' project and then add 'Form' in my project:

    Then I click and add 'Form.Load' and 'Form.FormClosing' event in 'HiddenForm':

    Finally I install the service and run it successfully.

    Hope it could be helpful.

    Besides, if you have any further questions, please let me know.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 11, 2020 8:06 AM
  • @Xingyu Zhao,

    Using your approach, I got the TimeChanged System Event to log something.  But it still didn't work for UserPreferenceChanged (which the documentation claims) or DisplaySettingsChanged (which is what I really want).

    Can you quickly verify on your end if these other system events work? UserPreferenceChanged, DisplaySettingsChanged

    Best regards,

    Tam

    Thursday, June 11, 2020 4:56 PM
  • I edited the HiddenForm_Load to write a log message at the end:

            private void HiddenForm_Load(object sender, EventArgs e)
            {
                EventLog.WriteEntry("WindowsServiceWithGuiTest.DisplaySettingsChanged", "HiddenForm_Load started");
                SystemEvents.TimeChanged += new EventHandler(SystemEvents_TimeChanged);
                SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(SystemEvents_UPCChanged);
                SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged;
                EventLog.WriteEntry("WindowsServiceWithGuiTest.DisplaySettingsChanged", "HiddenForm_Load ended");
            }

    The result of this change was that the EventViewer showed both messages, but I still was not getting the system events:

    HiddenForm_Load started

    HiddenForm_Load ended

    Thursday, June 11, 2020 5:31 PM
  • Hi Tam Bui,

    Thanks for your feedback.

    >>I got the TimeChanged System Event to log something.  But it still didn't work for UserPreferenceChanged (which the documentation claims) or DisplaySettingsChanged (which is what I really want).

    I make a test again and reproduce your problem: It didn't work for UserPreferenceChanged and DisplaySettingsChanged.

    In order to get more help about this problem, you can consider reporting your problem in developer community or give your feedback in MicrosoftDocs.

    Thank you for your understanding.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Tam Bui - Dev Friday, June 12, 2020 3:59 PM
    Friday, June 12, 2020 8:15 AM
  • Hi Xiangyu Zhao,

    Thank you for confirming for me that UserPreferenceChanged didn't work.  I will report the problem to both the developer community and MicrosoftDocs links that you provided.

    Regards,

    Tam

    Friday, June 12, 2020 4:28 PM