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.
C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Thank you all for your inputs, they certainly pointed me in the right direction. I was able to solve my issue and I figure to put them here for anyone down the road that find them helpful.
So in my main program where I run the service, it has to be a sub class of ServiceBase.
In my main function I spawned a separate thread as follow:
new Thread(RunMessagePump).Start();
private void RunMessagePump()
{
Application.Run(new HiddenForm())
}
This Hidden form is exactly what its name say, it is a simple form that is remain hidden by the code this.ResumeLayout(false) under its Intiailaizecomponent() code.
Within this form's FormLoad event you can declare any System event. In my case I have
private void HiddenForm_Load(object sender,EventArgs e)
{
SystemEvents.timeChanged += new EventHandler(SystemEvents_TimeChanged);
}
Voila, this works out beautifully, and I think I will use this form load event to capture any SystemEvents I want to use down the road.
Cheers.
Well I would strongly suggest not relying on the local time to start with. Use the UTC time of the server, and then if you need to convert that to the local time you can do that wherever you want to, if you know its time zone.
Detecting a manual time change without a useful event is slightly tougher, but could you just run a timer which executes every (say) 30 seconds and checks whether the UTC time isn't "UTC time at least timer tick + 30 seconds" with some amount of tolerance?
You don't need to run it terribly often, so it's not like it's going to be a huge waste of resources. You could always hide this behind an API which can be replaced with a more efficient one if you ever do find a better answer :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With