Here is how I change the TimeZoneInfo (App #1) :
private static void ChangeTimeZone(TimeZoneInfo tzi)
{
TIME_ZONE_INFORMATION actual = new TIME_ZONE_INFORMATION();
NativeMethods.GetTimeZoneInformation(out actual);
if (tzi == null || actual.StandardName == tzi.StandardName)
return;
TIME_ZONE_INFORMATION newZone = (TIME_ZONE_INFORMATION)tzi;
RunWin32Method(() => NativeMethods.SetTimeZoneInformation(ref newZone));
// Update .NET
CultureInfo.CurrentCulture.ClearCachedData();
TimeZoneInfo.ClearCachedData();
// Notify all windows that we changed a Windows setting.
// result is True
IntPtr ptr;
System.Diagnostics.Debug.WriteLine(NativeMethods.SendMessageTimeout(NativeMethods.HWND_BROADCAST, NativeMethods.WMI_SETTING_CHANGE,
IntPtr.Zero, IntPtr.Zero, 0x00, 1000, out ptr));
}
When I call my method:
ChangeTimeZone(TimeZoneInfo.GetSystemTimeZones().First(e => !e.SupportsDaylightSavingTime));
// Stopping debugger and watching other .NET App then continue to next instruction
ChangeTimeZone(TimeZoneInfo.GetSystemTimeZones().First(e => e.StandardName.Contains("Romance")));
Here is the other app (App #2):
static void Main(string[] args)
{
while (true)
{
Console.WriteLine(DateTime.Now);
Thread.Sleep(500);
}
}
Output of DateTime is never updated to the new TimeZone, why?
EDIT
As @Jon said, by adding CultureInfo.CurrentCulture.ClearCachedData();
the new date will be updated. But as said, I would that ALL other application uses this new TimeZone. I have a lot apps running in background using the DateTime.Now
, it would be bad to specify each time to clear the cache before retrieve the local updated date...