SetTimeZoneInformation不会再.NET应用程序更新DateTime.Now [英] SetTimeZoneInformation does not update DateTime.Now for another .NET Application

查看:377
本文介绍了SetTimeZoneInformation不会再.NET应用程序更新DateTime.Now的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我如何改变的TimeZoneInfo(附录1):

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));
}

当我把我的方法:

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")));

下面是其他应用程序(App#2):

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?

,正如@乔恩说,由加入 CultureInfo.CurrentCulture.ClearCachedData(); 新日期将被更新。但正如所说,我愿意全部其他应用程序使用此新的时区。我在后台运行了大量的应用程序使用 DateTime.Now ,那就要烂到指定每次清除缓存之前检索本地更新的日期...

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

推荐答案

我怀疑你的第二个应用就是使用缓存时区数据。 (这是在一个单独的进程,毕竟 - 清除高速缓存中的应用程序1不会影响应用程序2.任何过程中的高速缓存)尝试调用的 TimeZoneInfo.ClearCachedData 在app 2,看看是否能挑选出了问题。

I suspect your second app is just using cached time zone data. (It's in a separate process, after all - clearing the cache in app 1 isn't going to affect any in-process caches in app 2.) Try calling TimeZoneInfo.ClearCachedData in app 2 and see if that sorts out the issue.

这篇关于SetTimeZoneInformation不会再.NET应用程序更新DateTime.Now的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆