在.NET中:保持的CurrentCulture新的主题最好的方法是什么?最好的、方法、主题、NET

2023-09-02 20:47:17 作者:痴情少年痴情伤

在.NET 4.0 WPF 的项目,我们需要保持同样的CurrentCulture每个线程上那么我们在主线程中。

In a .Net 4.0 WPF project, we need to keep the same CurrentCulture on each thread then we have on the main thread.

考虑,我们可以初始化一个新的线程的文化与code这样的:

Given, we can initialize a new thread's culture with code like the following:

请一个变量(上下文)的信息

Keep the information in a variable (context)

context.CurrentCulture = Thread.CurrentThread.CurrentCulture;
context.CurrentUICulture = Thread.CurrentThread.CurrentUICulture;

国家统计局 以互联网为基础的文化新业态保持较快增长

在新的线程,从保存的上下文初始化

On the new thread, initialize from the saved context

Thread.CurrentThread.CurrentCulture = context.CurrentCulture;
Thread.CurrentThread.CurrentUICulture = context.CurrentUICulture;

但在这个年龄段的第三方物流,异步编程和lambda代表,它不适合我。

But in this age of TPL, async programming and lambda delegates, it does not feel right.

是的,我们实际上可以在应用程序运行时改变文化,但那是另外一回事了。

And yes, we actually can change the culture while the application is running, but that is another story.

你知道任何设置,财产或配置,我们应初始化跟踪?

Do you know of any setting, property or config we should initialize to keep track?

推荐答案

有没有什么好办法,避免这种不惜一切代价。的根本问题是,文化是不Thread.ExecutionContext的一部分,它不会从一个线程流向另一个。这是一个无法解决的问题,文化是一个原生的Windows线程的属性。在控制面板的区域和语言小程序选择它总是被初始化为制度文化。

There is no good way, avoid this at all cost. The fundamental problem is that culture is not part of the Thread.ExecutionContext, it doesn't flow from one thread to another. This is a unsolvable problem, culture is a property of a native Windows thread. It will always be initialized to the system culture as selected in Control Panel's Region and Language applet.

制作临时线程局部改变文化精品,努力转进程的另一种文化是错误你会狩猎个月的来源。该字符串归类顺序的问题,最恶劣的根源。

Making temporary thread-local changes to the culture is fine, trying to switch 'the process' to another culture is a source of bugs you'll be hunting for months. The string collation order is the nastiest source of problems.

编辑:这个问题已在.NET 4.5的CultureInfo.DefaultThreadCurrentCulture和DefaultThreadCurrentUICulture性能

this problem was fixed in .NET 4.5 with the CultureInfo.DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture properties.