.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 ...
随机推荐
- (转)Asp.Net MVC中身份认证和授权
MVC自带的ActionFilter 在Asp.Net WebForm的中要做到身份认证微软为我们提供了三种方式,其中最常用的就是我们的Form认证,需要配置相应的信息.例如下面的配置信息: < ...
- 一款基于jquery的下拉点击改变背景图片
今天给大家介绍一款基于jquery的下拉点击改变背景图片.单击右上角的图片,下拉显示可选择的背景图片,单击图片变为背景图.效果图下: 在线预览 源码下载 实现的代码. html代码: <a ...
- Linux清除磁盘上的RAID信息
我的笔记本是DELL的Inspiron 14z,原装存储器是由32G的固态硬盘和512G的机器硬盘组成.后来我自己又给它加了一个256G的固态硬盘,装并装上了CentOS,打算把500G的机械硬盘当资 ...
- MeepoPS——轻量级 Socket 服务
MeepoPS是Meepo PHP Socket的缩写.旨在提供高效稳定的由纯PHP开发的多进程SocketService. MeepoPS可以轻松构建在线实时聊天,即时游戏,视频流媒体播放,RPC, ...
- linux modprobe命令参数及用法详解--linux加载模块命令
转:http://www.linuxso.com/command/modprobe.html modprobe(module probe) 功能说明:自动处理可载入模块. 语 法:modprobe [ ...
- Java再学习——线程之创建
Java创建线程有两种方法,一种是继承Thread,另一种实现Runnable或Callable接口. 一,继承Thread public class APP { public static void ...
- 智能 RPC框架 (C++)
RPC中文叫远程函数调用,它是一种通信方式,只是看起来像普通的函数调用. 它包括三个基本要素: 1:服务端注册相应的(服务)函数(用于调用方调用) 2:调用方通过函数调用的方式将一些信息和参数打包到消 ...
- [JavaEE] Hibernate OGM
Hibernate Object/Grid Mapper (OGM)这个项目能够为NoSQL数据库提供Java Persistence(JPA)支持.它复用了Hibernate Core引擎将实体持久 ...
- Java基础知识强化之IO流笔记81:NIO之 DatagramChannel
1. Java NIO中的DatagramChannel是一个能收发UDP包的通道.因为UDP是无连接的网络协议,所以不能像其它通道那样读取和写入.它发送和接收的是数据包. 2. DatagramCh ...
- struts1标签-<html:radio> 使用注意事项
struts1标签-<html:radio> 使用注意事项 1.默认选中效果 在后台XxxForm中写入如下代码,可保证默认选中效果实现: @Override public void re ...