Rob Prouse
Rob Prouse

Reputation: 22657

Thread Creation Event for setting CurrentCulture

Our application allows the user to change the Culture that they run it under and that culture can be different than the underlying OS Culture. The only way that I have found of doing this is by setting Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture for every thread.

The only problem with this is that we must set the culture for every thread and we need to remember to do this whenever we create a new thread. If you set a thread to another culture, then create a new thread, it gets the culture of the OS, not of the thread that created it.

I would like to find a better way of doing this. I can only think of two things, but I don't know if either are possible.

  1. Set the culture at the application level so that all threads default to that culture. Is this possible?
  2. Is there a thread creation event anywhere that I can subscribe to? This way I can set up one handler to set the culture on thread creation.

Any ideas or help would be welcome, even if I need to PInvoke down to the Win32 API. Thanks in advance.

EDIT: I found this question which is similar, but no answer was found.

Upvotes: 4

Views: 1135

Answers (4)

B.Spangenberg
B.Spangenberg

Reputation: 400

Just to add to @Rob Prouse answer. Here is a quick demonstration.

  1. Setting the culture on App intialization :

      if(CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol != "N$")
      {
           CultureInfo customCulture = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
           customCulture.NumberFormat.CurrencySymbol = "N$";
    
           CultureInfo.DefaultThreadCurrentCulture = customCulture;
           CultureInfo.DefaultThreadCurrentUICulture = customCulture;
       }
    
  2. Using the culture info on my .ToString().

     .ToString("C", CultureInfo.DefaultThreadCurrentCulture)
    

Upvotes: 0

Rob Prouse
Rob Prouse

Reputation: 22657

Two new properties were added to the CultureInfo class in .NET 4.5 which solve this problem, DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture.

Now you can set these two properties and all new threads will be set to the default culture instead of the system culture. These properties will also set the culture for all existing threads that do no explicitly have their culture set.

Upvotes: 1

Jason Jackson
Jason Jackson

Reputation: 17260

  1. You could place all of your culture logic and static resource files in the UI/Presentation layer and only worry about the UI thread's culture (assuming Desktop app here). This would include any translations that are in resource files, any culture-specific formatting, etc.
  2. If the first idea cannot be implemented, you might create a thread helper class that sets the culture for you when long-running threads are created. You will also need to set the culture of all the thread-pool threads.

Culture is a thread-specific concept in Windows, and threads in Windows are actually agnostic of a process or appdomain. There is no level of abstraction between a thread and the OS that holds culture info (that I know of), so you have to set it on each thread.

Upvotes: 1

Gant
Gant

Reputation: 29899

May not be the exact solution, but how about creating a class that responsible for your thread creation ?

class MyThreadFactory
{
    public static Thread getThread()
    {
        Thread a = new Thread(..);
        a.CurrentCulture = xxxx;
        return a;
    }
}

Usage:

Thread newThread = MyThreadFactory.getThread();

Upvotes: 1

Related Questions