Thread & CurrentCulture

Recently I had quite interesting situation I was not fully aware of. The problem was, that user got the message from my application in one language, although he started application with another one.
What had happened was – the operation was executed in another thread, which had different culture settings (specific to local machine) than culture settings of my application – as a result other resources where read 🙂

An example may be ilustrated with following code:
ThreadCultureCode

And here is the result.
ThreadCultureCodeResult

This can lead of course to unexpected situations, when we do culture specific operations in the new thread. Passing current culture information to the new thread may be a solution, however doing it each time may be a little bit tiring -.-‘
Fortunately, since .NET 4.5 we get one property, which allows us to set the default culture for new threads in a simple, ease and clear way.
After such modification to the code:
ThreadCultureCodeModification
We get expected result:
ThreadCultureCodeModificationResult

Now all we have to do is to convince stakeholders to allow us to use .NET 4.5 what is not always an easy task 😉


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.