Why is CurrentCulture a Thread Property?

It seems to me a strange design choice that the current culture information (CurrentCulture and / or CurrentUICulture) is a property of the working thread. At least it seems that the volume of such a thing should be at one level up, at the process level.

But these things usually make sense when you hear the rationale. It might be interesting to know why the .NET developers decided that Thread was the right place to host this property.

+7
design cultureinfo currentculture
source share
4 answers

Firstly, it was not their choice. This decision was made long before they began work, and culture is a property of the operating system thread. Check out the SDK docs for the Get / SetThreadLocale () API functions, for example.

This does not fully explain this; they virtualized other OS functions, although it is very difficult to virtualize, because many APIs are culture sensitive, especially COM versions.

The next good reason is that it is very difficult to alter the process across the whole culture. This is a state of insoluble race. Some other thread may be at the center of formatting culturally similar data. While blocking will work to prevent the use of the properties of a culture as it changes, it cannot prevent it from changing in the middle of a call formatting chain. This would require them to take some kind of global lock and hold it for the duration of the formatting job. Probably a dead end.

There is another aspect of this, which I consider to be a real problem. This is due to the Thread.ExecutionContext property. The structure uses this to “stream” the state of a stream from one stream to another. Very obscure, but important to stuff things like the security context into the workflow. It would be ideal if this context could also fill the culture, so you can be sure that any of the workers you start has the same culture that you have chosen, and not the default for the operating system.

This is not so, I really do not know why. Probably because the first reason I gave. However, it is very dangerous for changing the culture of flows. The errors that can cause are very subtle. Similar to creating a SortedDictionary with a string as a key element of the main thread with a custom culture by default. Then, finding out that the workflow cannot occasionally find material back, because the rules for sorting strings are different.


EDIT: There is some relief from this problem in .NET 4.5, it supports the new static properties CultureInfo.DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture.


EDIT2: The culture is now flowing as described in the 4th paragraph in .NET 4.6. This should alleviate all problems.

+10
source share

I'll take a picture - maybe because you need several simultaneous streams using a different culture? If you are thinking of the asp.net multilingual website, you don’t want the process to be bound to one language ... the web request could be EN-US and another fr-FR.

+6
source share

Window handlers per se refer to a stream. You should never use a window handle (for a top-level window, child control, etc.) in the context of another thread, because code that implements window processing (GDI) is not thread safe in itself. Since UICulture is a specific window, this means that it is also defined at the thread level.

Other aspects of the GUI also relate to the stream, such as the active window and focus. Although there is an API, I can’t remember a name that combines a user interface context from different threads together so that the focus, active window is shared between several threads of the user interface.

+2
source share

CurrentCulture is nothing more than a link to CultureInfo, so not much would be lost, allowing it to hang in the stream (only memory for one link). At the same time, you get the opportunity to allow users to precisely control the current culture in the stream, which is important in some types of applications.

One of the reasons this fine grain may be more obvious to non-English users about modern software. I need to switch keyboards regularly on Windows, and I can do this with one global keyboard shortcut. Having CultureInfo in the stream allows us to easily implement the same logic in our .NET applications without the need to change this information for each process.

Examples of use that come to mind:

  • I may need a shortcut that will change the culture in my program for a short period of time upon request for the current user (many applications must allow multiple users on the same workstation and in the same session). But the same process may have to continue to use the default CultureInfo value on the machine, for example. log is always with the same temporary type.

  • Perhaps you also have a reporting solution that will produce reports for several branches of a global company, each of which has its own culture. Your design will be less limited if you can change CultureInfo for each stream.

  • Another reason is that you want to write to the file using a specific culture, but do not want to specify the culture in your recording functions (for example, since these functions are already written). Then you can easily change this logic without changing it on other threads that can do work that requires a default culture.

+2
source share

All Articles