I run an application as a service on a server and then I have multiple clients connect to that service. I display the exact server time on each of the client Window form application and because of that I need a mechanism to detect any time change on the server so that I can send it to the client to display.
Looking over the MSDN I found SystemEvents.TimeChanged , I thought I'm in luck because when testing with the code below with Visual Studio everything works great:
SystemEvents.TimeChanged += new EventHandler(SystemEvents_TimeChanged);
static void SystemEvents_TimeChanged(object sender, EventArgs e)
{
//Occurs when user manually changes the time or due to
//daylight saving time. Server will issue its latest time
//to all the clients so time can be adjusted.
}
Unfortunately when I run this as a service, this event will never get fired because looking at the fine print on MSDN, the TimeChanged event only fires in an application with a message pump (aka it has to have a running form) active. It works in Visual Studio because it has a form when I run it as a non service mode.
I can correct this by checking the option "Allow service to interact with desktop" but that kind of defeats the purpose of me trying to run the application as a service.
I could create a timer tick event in the application to check for time change, let's say every 10 seconds or so but this is a big overhead just to check for a random time change that occurs on the server machine twice or maybe 3 times per year.
So anyone know a C# event that trigger on time change that works in an application that does not have message pump going? I'm surprise if Microsoft does not include an event that works this way
Thank you.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…