Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

dst - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...