code save
code save

Reputation: 1106

System event timer_changed is not called in Windows service

Unable to call this system event in Windows service, how can we call it in Windows service?

 Microsoft.Win32.SystemEvents.TimeChanged += SystemEvents_TimeChanged;
void SystemEvents_TimeChanged(object sender, EventArgs e)
        {
            AnyMethodExample();            
        }

Upvotes: 0

Views: 411

Answers (2)

S.N
S.N

Reputation: 5140

It depends where you are calling within windows service. You can post complete code for further support. Simply,

class Program
    {
        static void Main(string[] args)
        {
            SystemEvents.TimeChanged += new EventHandler(SystemEvents_TimeChanged);
            Console.ReadKey();
        }

        static void SystemEvents_TimeChanged(object sender, EventArgs e)
        {
            Console.WriteLine("TimeChanged: {0}", DateTime.Now);
        }
    }

Caution: Because this is a static event, you must detach your event handlers when your application is disposed, or memory leaks will result.

Upvotes: 0

Nitin Purohit
Nitin Purohit

Reputation: 18578

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.

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

Upvotes: 1

Related Questions