以下是我更改 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));
}
当我调用我的方法时:
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")));
这是另一个应用程序(应用程序#2):
static void Main(string[] args)
{
while (true)
{
Console.WriteLine(DateTime.Now);
Thread.Sleep(500);
}
}
DateTime 的输出永远不会更新到新的时区,为什么?
EDIT
正如@Jon所说,通过添加CultureInfo.CurrentCulture.ClearCachedData();
新日期将会更新。但正如我所说,我愿意ALL其他应用程序使用这个新的时区。我有很多应用程序在后台运行DateTime.Now
,每次指定在检索本地更新日期之前清除缓存会很糟糕......
我怀疑您的第二个应用程序只是使用缓存的时区数据。 (毕竟,它位于一个单独的进程中 - 清除应用程序 1 中的缓存不会影响应用程序 2 中的任何进程内缓存。)尝试调用TimeZoneInfo.ClearCachedData http://msdn.microsoft.com/en-us/library/system.timezoneinfo.clearcacheddata.aspx在应用程序 2 中查看是否可以解决问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)