Alex
Alex

Reputation: 2382

System.DateTime and Caching of TimeZoneInfo and CultureInfo

I have two separate .NET applications, one server and another WinForms. WinForms app has a clock running showing the current Time from DateTime.Now. The Server app can change TimeZone of the box. I need to notify the WinForms app that the TimeZone has changed so that the Clock control can clear it's cache and show correct time. I can send it a message, but i am looking for a standard way how it's done in Windows. What's the best approach? Is registering message in both and watching for it in WndProc it?

Upvotes: 2

Views: 1116

Answers (2)

Alex
Alex

Reputation: 2382

Ok, so i am going to answer my own question, since it was specifically about system events and the previous answer gave a good, but generic solution. Basically, what i found out is that there is a Microsoft.Win32.SystemEvents class that has a TimeChanged event. All you have to do in your application is do something like the following:

SystemEvents.TimeChanged += (s, e) =>
        {
            CultureInfo.CurrentCulture.ClearCachedData();
            TimeZoneInfo.ClearCachedData();
        };

This will invalidate DateTime's cache and next time you poll the time, you will have the correct time in your DateTime.Now structure.

Upvotes: 2

casperOne
casperOne

Reputation: 74560

Since they are both .NET applications, I recommend using a communications technology, such as Windows Communication Foundation to enable communication between the applications.

Your server would set up a ServiceHost which your WinForms applications would have a proxy to the service you set up.

You would want to design your service so that your client could pass a callback so that when the time zone changes on the server, it can notify the clients.

Upvotes: 0

Related Questions