multi threaded wpf application setting thread cultureinfo

给你一囗甜甜゛ 提交于 2019-12-24 06:16:40

问题


I'm working on a multithreaded wpf application. To perform globalization i tried to set the current thread's(main thread) culture to invariant culturee in app.xaml.cs, So that all C# objects in app domain works on culture invariant info. But the problem arises when many threads comes to usage the worker threads invoked, those thread's culture are defaulted to OS Culture settings which i don't want. Help me in finding out a way where the worker threads created inherits the CultureInfo from main thread


回答1:


I don't think there is a way to assign the culture to the entire AppDomain. Your best option might be to use a helper class to instantiate the threads.

class ThreadHelper
{
    public static Thread getThread(ThreadStart start, int maxStackSize)
    {

        Thread t = new Thread(start,maxStackSize);
        t.CurrentCulture = new System.Globalization.CultureInfo(3081);
        return t;
    }
}



回答2:


Do this:

if (Thread.CurrentThread.CurrentCulture.Name != "en-US")
{
    var culture = CultureInfo.CreateSpecificCulture("en-US");
    CultureInfo.DefaultThreadCurrentCulture = culture;
    CultureInfo.DefaultThreadCurrentUICulture = culture;
}

As you can see it this sets your default culture for both background and UI threads. If you drop this in your app.xaml then you should be all set.



来源:https://stackoverflow.com/questions/4159835/multi-threaded-wpf-application-setting-thread-cultureinfo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!