如何更改在.NET中的整个过程(而不仅仅是当前线程)的的CurrentCulture?而不、仅仅是、线程、整个过程

2023-09-02 10:51:51 作者:[心赠于你]

我有一个情况我需要把我的进程的语言环境为en-US。

I have a situation where I need to set my process' locale to en-US.

我知道如何做到这一点当前线程:

I know how to do this for the current thread:

System.Threading.Thread.CurrentThread.CurrentCulture = 
     System.Globalization.CultureInfo.CreateSpecificCulture("en-US");

但我的应用程序使用 BackgroundWorkers 做一些处理,并现场为这些工作线程似乎并没有受到上述改变其产卵的主线程。

But my application uses BackgroundWorkers to do some processing, and the locale for these worker threads seems not to be affected by the above change to their spawning main-thread.

所以,我怎么可以设置区域设置为我的应用程序中的所有线程无需手动设置它在每一个?

So how can I set the locale for all the threads in my application without setting it in each one manually?

推荐答案

您将不得不改变操作系统的语言环境,如果你想这样做。是什么原因,你想BackgroundWorkers为en-US运行?

You'll have to change the operating system locale if you want to do that. For what reason do you want BackgroundWorkers to run in en-US?

您应该有你的业务层运行在一个不变的文化,并且只对最终用户的用户界面特定的文化。

You should have your business layer running in an invariant culture, and only have a specific culture for the end user's UI.

如果您使用的是BackgroundWorker分量,不得不这样做,你可以尝试这样的事情在DoWork的方法:

If you are using the BackgroundWorker component, and have to do this you could try something like this in the DoWork method:

// In DoWork
System.Globalization.CultureInfo before = System.Threading.Thread.CurrentThread.CurrentCulture;
try

{
    System.Threading.Thread.CurrentThread.CurrentCulture = 
        new System.Globalization.CultureInfo("en-US");
 // Proceed with specific code
}

finally
{
    System.Threading.Thread.CurrentThread.CurrentUICulture = before;
}
 
精彩推荐
图片推荐