Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 828
Assaf Lavie Avatar asked Sep 22 '09 13:09

Assaf Lavie


People also ask

How do I change the current culture in C#?

WriteLine("CurrentCulture is {0}.", CultureInfo.CurrentCulture.Name); // Change the current culture to th-TH. CultureInfo. CurrentCulture = new CultureInfo("th-TH", false); Console. WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name); // Display the name of the current UI culture.

How do I change my UI culture?

To change the current UI culture, you assign the CultureInfo object that represents the new UI culture to the Thread. CurrentThread. CurrentUICulture property.

What is UICulture?

UICulture affects which resource file (Resources. lang. resx) is going to be loaded to by your application. So to load German resources (presumably localized text) you would set UICulture to the German culture and to display German formatting (without any impact on which resources are loaded) you would set Culture .


1 Answers

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;
}
like image 56
RichardOD Avatar answered Sep 30 '22 06:09

RichardOD