Search code examples
c#servicesystemtime

C# service registration to systemevent.Timechange()


I wrote a c# service that register to systemevent.Timechange() with function that call OntimeChange() (as mentioned in the MSDN http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.aspx) Running this code in visual studio all works fine (In debug or release mode), but when I run the service I saw that the function OntimeChange() isn't called (I added prints to the log in this function, and I saw that nothing printed to the log)

can someone help?


Solution

  • In the remarks section of the SystemEvents.TimeChanged event you can read the following text:

    Note

    This 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. For a code example that shows how to handle system events by using a hidden form in a Windows service, see the SystemEvents class.

    So in short, you need a hidden form to receive these events in a service. See the Example 2 listed on the page you linked in your question on how to do that.