国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

.NET4.5可以給所有線程設置默認的Culture了

2019-11-17 01:29:38
字體:
來源:轉載
供稿:網友

.NET4.5可以給所有線程設置默認的Culture了

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 would need to set the culture in somewhere like application bootstrap.

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

線程默認的是用服務器的Cultrue

.NET4.5之前,只能用以下代碼只能針對單個線程,如果每次執行線程都要重新設置一下。。。

Syste
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 called DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture. 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就簡單了,在網站啟動的時候 ,設置一下這個,所有線程都會用這個默認的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!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 璧山县| 应城市| 顺义区| 安平县| 讷河市| 灵川县| 天台县| 咸阳市| 新绛县| 绍兴县| 沁阳市| 团风县| 桂东县| 永春县| 讷河市| 乌拉特中旗| 清水河县| 房山区| 凌云县| 大英县| 康平县| 上栗县| 屏东县| 分宜县| 英德市| 宣武区| 凤山县| 昭觉县| 德昌县| 陆河县| 巴青县| 屏山县| 淮北市| 乐清市| 闵行区| 卢湾区| 洞头县| 新昌县| 河北区| 景宁| 临城县|