-2

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?

Fischermaen
  • 12,238
  • 2
  • 39
  • 56
gln
  • 1,011
  • 5
  • 31
  • 61
  • 2
    Please show some code rather than a verbal description of it. – Adam Robinson Dec 12 '11 at 12:44
  • Is this a windows service? Do you have a hidden form as described here: http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.timechanged.aspx – kmp Dec 12 '11 at 12:44

2 Answers2

1

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.

Frank Bollack
  • 24,478
  • 5
  • 49
  • 58
  • i have hidden form and in debugging all worrk!! private void HiddenFormLoad(object sender, EventArgs e) { SystemEvents.TimeChanged += _onTimeChanged; } – gln Dec 12 '11 at 13:06
  • Then you should update your question and show some of your code. Can you verify that the form is actually running in the background (e.g. traces in the event log from the forms ctor or HiddenForm_Load() method)? – Frank Bollack Dec 12 '11 at 13:52
0

use AppDomain.CurrentDomain.BaseDirectory

akjoshi
  • 15,374
  • 13
  • 103
  • 121
levbohdan
  • 1
  • 1