.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!
.NET4.5可以给所有线程设置默认的Culture了的更多相关文章
- tomcat 7.0 最大连接数和线程设置
部署项目时需要根据服务器配置调整连接数 <Connector port="8080" protocol="HTTP/1.1"URIEncoding=&qu ...
- datepickerx设置默认日期
datepicher插件是jQuery UI的一个插件,它提供一个日期弹出窗口(或直接显示在页面),供用户选择日期.在Web开发中,总会遇到需要用户输入日期的情况.一般都是提供一个text类型的inp ...
- 选择目录,选择文件夹的COM组件问题。在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式。请确保您的 Main 函数带有 STAThreadAttribute 标记。 只有将调试器附加到该进程才会引发此异常。
异常: 在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式.请确保您的 Main 函数带有 STAThreadAttribute 标记. 只有将调试器附加到该进程才会引发此异常. ...
- .NET DateTime类型变量作为参数时设置默认值
一个小的 Tips. .NET 中函数参数的默认值需要是编译时常量.如果参数是引用类型,可以设置Null,如果是值类型,可以设置相应的编译时常量,如整型可以用整数,但对于DateTime(结构体,值类 ...
- ng-option指令使用记录,设置默认值需要注意
ng-options一般有以下用法: 数组作为数据源: label for value in array select as label for value in array label group ...
- IIS设置默认主页无效
服务器系统:Windows server 2008 R2 IIS版本:7.5 IIS中部署一个dotnet framework 3.5的网站应用程序,设置"默认文档"为:index ...
- 《Entity Framework 6 Recipes》中文翻译系列 (14) -----第三章 查询之查询中设置默认值和存储过程返回多结果集
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 3-6在查询中设置默认值 问题 你有这样一个用例,当查询返回null值时,给相应属性 ...
- EF Core1.0 CodeFirst为Modell设置默认值!
当我们使用CodeFirst时,有时候需要设置默认值! 如下 ; public string AdminName {get; set;} = "admin"; public boo ...
- cefsharp设置默认语言
cefsharp是不错的浏览器内核封装版本之一,默认语言是en-US,这个一直困扰着项目,项目好多处需修改,后来经多次尝试,才发现,原来设置默认语言这么简单. CefSharp.Settings se ...
随机推荐
- C语言中将数字转换为字符串的方法
C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转换为字符串的一个例子: # include <stdio. h># ...
- javass 视频笔记二 (关键字,标示符,常量变量,运算符和if-else)
1,java的关键字和标识符2,java的基本数据类型3,变量和常量4,java的运算符5,if-else if - else表达式1,java的关键字和标识符 1.1,所有关键字都要小写 ...
- [Javascript] 面向对象编程思想
1.创建对象 1.1 new 用new进行创建对象: var user = new Object(); user.age = 12;//同时为对象添加属性 user.name = 'ajun'; 1. ...
- ls与dir的区别
1.ls具有上色的效果,dir没有. 2.ls是unix/linux系列的命令,dir是dos/windows系列的命令.
- 使用PIL处理image
获得一个Image实例 import Image im = Image.open('1.jpg') #返回一个Image对象,open只对图片的头做处理,所以open操作是非常快的 resize,裁剪 ...
- 《Cortex-M0权威指南》之体系结构---异常和中断
转载请注明来源:cuixiaolei的技术博客 异常会引起程序控制的变化.在异常发生时,处理器停止当前的任务,转而执行异常处理程序,异常处理完成后,会继续执行刚才的任务.异常分为很多种,中断是其中之一 ...
- Oracle基础 exp/imp和expdp/impdp的区别:
一.exp/imp和expdp/impdp在功能上的区别: 1.把用户usera的对象导入到userb emp/imp用法: formuser=usera touser=userb; empdp/im ...
- spring事务管理出错。No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy ...
- Java Scoket编程
Java Scoket编程 一,网络编程中两个主要的问题 一个是如何准确的定位网络上一台或多台主机,另一个就是找到主机后如何可靠高效的进行数据传输. 在TCP/IP协议中IP层主要负责网络主机的定位, ...
- Oracle 基础 —SQL语句优化的途径
一:SQL语句的优化途径 1.选择合适的Oracle优化器 (1).RBO 基于规则进行优化的优化器 --Oracle 10G 后被抛掉 (2).CBO基于成本(CPU 和 内存的占用率)优化的优 ...