简体   繁体   中英

C# event to detect Daylight Saving or even manual time change

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.

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 :)

也许不太理想,你可以在前台运行一个代理来监听TimeChanged事件并将其转发给你的服务。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM