問題描述
我需要將進程的語言環(huán)境設(shè)置為 en-US.
I have a situation where I need to set my process' locale to en-US.
我知道如何為當(dāng)前線程執(zhí)行此操作:
I know how to do this for the current thread:
System.Threading.Thread.CurrentThread.CurrentCulture =
System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
但我的應(yīng)用程序使用 BackgroundWorkers
進行一些處理,并且這些工作線程的語言環(huán)境似乎不受上述對其產(chǎn)生的主線程的更改的影響.
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.
那么如何在不手動設(shè)置每個線程的情況下為我的應(yīng)用程序中的所有線程設(shè)置語言環(huán)境?
So how can I set the locale for all the threads in my application without setting it in each one manually?
推薦答案
如果您想這樣做,您必須更改操作系統(tǒng)區(qū)域設(shè)置.您希望 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?
您的業(yè)務(wù)層應(yīng)該在不變的文化中運行,并且只有最終用戶的 UI 具有特定的文化.
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;
}
這篇關(guān)于如何在 .Net 中更改整個進程(不僅僅是當(dāng)前線程)的 CurrentCulture?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!