Have you ever had a problem that your multithreaded application lost a culture context ? Have you found a good solution for this?
I did. My application had to support different cultures. So I simply set the required culture on my application’s current thread like this
Then I noticed that worker threads, that I am using somewhere during the application runtime, don’t know anything about main thread’s culture. As it turned out they get the culture information from the system, not application’s main thread. Ok, to fix that I simply passed the CultureInfo to each new thread and set its culture, so it matches the main application’s thread.
However, since .Net Framework 4.5 there is a more elegant and efficient solution for this. The DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture properties introduced on CultureInfo class are the keys. The description on msdn says that the value of those properties is used as a default for every threads in application domain. When we assign a null then the system culture is used as a default, as it was before.
Great! Just two extra lines of code and everything works like expected: