R – Thread Creation Event for setting CurrentCulture

cultureglobalizationmultithreadingnet

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.

Best Answer

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