views:

516

answers:

1

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 effected 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...)?

+4  A: 

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;
}
RichardOD
The background worker may very well be running in the UI layer (as a `BackgroundWorker` component on a `Form` for instance)
Fredrik Mörk
@Fredrik- Yes (especially if it is the Background Worker component), but you don't update the UI on the background worker thread. At least you don't if you are using it properly.
RichardOD
The background worker does double.Parse(string) on some input, which breaks for certain locales that have weird decimal point representations. So, I still need to know how to set the locale for the entire process. Are you saying this isn't possible?
Assaf Lavie
Assaf- can you post a code sample. Do none of my suggestions work for you?
RichardOD
I ended up inheriting from BW and setting the CurrentCulture before every thread function...
Assaf Lavie
Assaf- sounds like a good approach- especially if you want to reuse the functionality in other places.
RichardOD