How can I change the CurrentCulture of the entire process (not just current thread) in .Net?

Assaf Lavie picture Assaf Lavie · Sep 22, 2009 · Viewed 42.3k times · Source

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");

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?

Answer

RichardOD picture RichardOD · Sep 22, 2009

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.

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;
}