【问题标题】:Change thread culture .NET Framework 4.6 and later versions更改线程区域性 .NET Framework 4.6 及更高版本
【发布时间】:2018-06-23 22:43:31
【问题描述】:

我正在尝试更改一个新线程 CultureInfo,如下面的示例,但是:

  • Attempt1:SetCulture1() 没有更改我的线程
  • Attempt2: SetCulture2() 我收到异常“System.InvalidOperationException: instance is read-only”(设置 CurrencyDecimalSeparator 时)

    static void Main(string[] args)
    {
        Thread th = new Thread(thread_test);
    
        // nothing happens
        SetCulture1(th);
    
        // exception System.InvalidOperationException: instance is read-only
        SetCulture2(th);
    
        th.Start();
    }
    
    public static void SetCulture1(System.Threading.Thread thread)
    {
        var ci = new System.Globalization.CultureInfo("pt-BR");
        ci.NumberFormat.CurrencyDecimalSeparator = ".";
    
        thread.CurrentCulture = ci; // <-- after this culture info not change
    
        if (thread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator != ".")
        {
            Console.WriteLine("Nothing happened");
            Console.ReadKey();
        }
    }
    
    public static void SetCulture2(System.Threading.Thread thread)
    {
        thread.CurrentCulture = new System.Globalization.CultureInfo("pt-BR");
        thread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator = "."; // <-- exception throws here
    }
    
    static void thread_test()
    {
        Console.WriteLine("Culture: {0}", CultureInfo.CurrentCulture.DisplayName);
    }
    

我注意到,在 .net 4.6 之前,此示例有效。 4.6 有什么变化吗?

谢谢!

【问题讨论】:

标签: c# .net multithreading cultureinfo


【解决方案1】:

虽然这种奇怪的情况没有答案 (Microsoft bug report),但我找到了一个解决方法,可以在代码开头设置 DefaultThreadCurrentCulture(Main 方法):

        var ci = new System.Globalization.CultureInfo(System.Globalization.CultureInfo.CurrentCulture.LCID);
        ci.NumberFormat.CurrencyDecimalSeparator = ".";
        ci.NumberFormat.CurrencyGroupSeparator = ",";
        ci.NumberFormat.NumberDecimalSeparator = ".";
        ci.NumberFormat.NumberGroupSeparator = ",";
        ci.NumberFormat.PercentDecimalSeparator = ".";
        ci.NumberFormat.PercentGroupSeparator = ",";
        System.Globalization.CultureInfo.DefaultThreadCurrentCulture = ci;
        System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = ci;

【讨论】:

  • 它没有答案,因为没有错误。如果有,任何应用程序都无法在美国以外的地方使用。也不会有任何关于why can't I parse that doublehow do I convert that date 的问题。人们会注意到
  • @PanagiotisKanavos,在 .net 4.6 之前我的问题示例有效。仅当您尝试在 Start() 新线程之前更改任何区域性属性时才会出现此问题..
  • 您正在修改文化从另一个线程,而不是在线程开始之前。这是不支持的。来自 Thread 对象的 Culture and Threads 部分:The CurrentCulture and CurrentUICulture properties don't work reliably when used with any thread other than the current thread.
  • 人们不会受到影响,因为他们很少以这种方式工作。好的做法是将文化作为参数传递。 ASP.NET,任务线程来自池,文化由运行时或中间件管理。全局更改通常在 web.config 或 app.config 中进行。当需要自定义文化时,人们使用 CustomAndRegionInfoBuilder as shown here
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-14
  • 1970-01-01
  • 1970-01-01
  • 2017-06-04
  • 1970-01-01
  • 1970-01-01
  • 2020-07-18
相关资源
最近更新 更多