. NET4.5 you can set the default Culture for all threads.

Source: Internet
Author: User

How to set CurrentCulture for all threads in a domain in. NET 4.5

Before. NET 4.5 if we wanted to set CurrentCulture for the current thread, we wowould need to set the culture in somewhere like application bootstrap.

For example in the following code we set the culture to en-us:

By default, the thread uses the server's Cultrue

Before. NET4.5, the following code can only be used for a single thread. If you need to reset the code for each thread...

 
m.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");

 

For single threaded application and for regular usages, the preceding code works fine and there is nothing to be worry about. but problems come to the surface if we are working on a multi-threaded application which needs to have more than one thread.

Basically in. NET platform thread culture is read from Windows system culture by default and if the system culture is different from application culture, you need to somehow set the thread's culture manually for every threads.

The same problem is even there for Tasks in Task Parallel Library which means task's operations are done in the context of the Windows system thread by default. so we need to set the current culture manually inside of each task if we are doing something relevant to localization and so on.

Fortunately in. NET 4.5 a couple of cool properties have been added to the CultureInfo class calledDefaultThreadCurrentCultureAndDefaultThreadCurrentUICulture. A default culture will be set to the whole domain and for all threads by assigning a culture to these properties in application bootstrap.

. NET4.5 is simple. When the website is started, set this so that all threads will use this default Culture!

 
System.Globalization.CultureInfo.DefaultThreadCurrentCulture = new System.Globalization.CultureInfo("en-US");System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = new System.Globalization.CultureInfo("en-US");
[assembly: System.Web.PreApplicationStartMethod(typeof(Web.AppStart), "Main")]namespace Web{        public class AppStart    {        public static void Main()        {            //set default culture here                  }    }}

 

 

Have fun!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.