如何更改在.NET中的整个过程(而不仅仅是当前线程)的的CurrentCulture? [英] How can I change the CurrentCulture of the entire process (not just current thread) in .Net?
问题描述
我有一个情况我需要把我的进程的语言环境为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.
如果您使用的是<一href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx">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;
}
这篇关于如何更改在.NET中的整个过程(而不仅仅是当前线程)的的CurrentCulture?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!