views:

487

answers:

2

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.

+1  A: 

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();
m3rLinEz
+1  A: 
  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.

Jason Jackson