Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In .Net: best way for keeping CurrentCulture on new Thread?

Tags:

In a .Net 4.0 WPF project, we need to keep the same CurrentCulture on each thread then we have on the main thread.

Given, we can initialize a new thread's culture with code like the following:

  1. Keep the information in a variable (context)

    context.CurrentCulture = Thread.CurrentThread.CurrentCulture; context.CurrentUICulture = Thread.CurrentThread.CurrentUICulture; 
  2. On the new thread, initialize from the saved context

    Thread.CurrentThread.CurrentCulture = context.CurrentCulture; Thread.CurrentThread.CurrentUICulture = context.CurrentUICulture; 

But in this age of TPL, async programming and lambda delegates, it does not feel right.

And yes, we actually can change the culture while the application is running, but that is another story.

Do you know of any setting, property or config we should initialize to keep track?

like image 922
PBelanger Avatar asked Feb 28 '11 16:02

PBelanger


People also ask

What is CultureInfo InvariantCulture in c#?

The CultureInfo. InvariantCulture property is used if you are formatting or parsing a string that should be parseable by a piece of software independent of the user's local settings. The default value is CultureInfo. InstalledUICulture so the default CultureInfo is depending on the executing OS's settings.

How do I change the current culture in C#?

using namespace System; using namespace System::Globalization; using namespace System::Threading; int main() { // Display the name of the current thread culture. Console::WriteLine("CurrentCulture is {0}.", CultureInfo::CurrentCulture->Name); // Change the current culture to th-TH.

What is Currentuiculture?

CurrentCulture is the . NET representation of the default user locale of the system.

What is C# CultureInfo?

The CultureInfo class provides culture-specific information, such as the language, sublanguage, country/region, calendar, and conventions associated with a particular culture. This class also provides access to culture-specific instances of the DateTimeFormatInfo, NumberFormatInfo, CompareInfo, and TextInfo objects.


1 Answers

There is no good way, avoid this at all cost. The fundamental problem is that culture is not part of the Thread.ExecutionContext, it doesn't flow from one thread to another. This is a unsolvable problem, culture is a property of a native Windows thread. It will always be initialized to the system culture as selected in Control Panel's Region and Language applet.

Making temporary thread-local changes to the culture is fine, trying to switch 'the process' to another culture is a source of bugs you'll be hunting for months. The string collation order is the nastiest source of problems.


EDIT: this problem was fixed in .NET 4.5 with the CultureInfo.DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture properties.


EDIT: and got the real solution in .NET 4.6, culture now flows from one thread to another. Check the MSDN article for CultureInfo.CurrentCulture for details. Beware that the description does not quite match behavior, testing required.

like image 153
Hans Passant Avatar answered Oct 21 '22 10:10

Hans Passant