Introduction

As we all know, when we run a .NET application, it will use the culture installed in the machine. But sometimes, we want to run our application in a specific culture. In the beginning, I thought it would be very simple, by setting the CurrentCulture and CurrentUICulture of the application's thread to the culture we want to use. But, if there is more than one thread running in our application, we will have a problem: the culture of the other threads would still be the installed culture of the machine. Theoretically, we can set the CurrentCulture and CurrentUICulture properties of a thread when we start it. But, actually, in some cases, we cannot know exactly when a thread is created or started if it is in a ThreadPool or is created by the Invoke/BeginInvoke method of a Control or a Delegate.

Background

Using the Code

In my demo application, I resolved these problems by creating a class named CultureManager. It has a static property, CurrentCulture, which stores the value of the current culture used by the application.

Because we always need the call to the SetThreadCulture method after creating a new thread, I simplify it by adding some CreateThread methods to create threads in the class. There are four CreateThread methods, corresponding to the four versions of the constructor of the Thread object.

Next, we have to look for a way to set the culture for the threads created by Invoke/BeginInvoke methods of a Control or a Delegate. This is not very easy because we don't know when this kind of threads are created and started. In case the invoker is a Control, the problem can be resolved because the created thread will use the culture of that control. But with a Delegate we create manually, we have to add a call to set the culture of the thread at the beginning of the method represented by that delegate. An example is (the call is in bold text):