如何在 .Net 中更改整个进程(不仅仅是当前线程)的 CurrentCulture?

How can I change the CurrentCulture of the entire process (not just current thread) in .Net?(如何在 .Net 中更改整个进程(不仅仅是当前线程)的 CurrentCulture?)

本文介绍了如何在 .Net 中更改整个进程(不仅仅是当前线程)的 CurrentCulture?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将进程的语言环境设置为 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?

您的业务层应该在不变的文化中运行,并且只有最终用户的 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;
}

这篇关于如何在 .Net 中更改整个进程(不仅仅是当前线程)的 CurrentCulture?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 .Net 中更改整个进程(不仅仅是当前线程)的 CurrentCulture?

基础教程推荐