How can I change the CurrentCulture of the entire process (not just current thread) in .Net?(如何在 .Net 中更改整个进程(不仅仅是当前线程)的 CurrentCulture?)
问题描述
我需要将进程的语言环境设置为 en-US.
I have a situation where I need to set my process' locale to en-US.
我知道如何为当前线程执行此操作:
I know how to do this for the current thread:
System.Threading.Thread.CurrentThread.CurrentCulture =
System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
但我的应用程序使用 BackgroundWorkers
进行一些处理,并且这些工作线程的语言环境似乎不受上述对其产生的主线程的更改的影响.
But my application uses BackgroundWorkers
to do some processing, and the locale for these worker threads seems not to be affected by the above change to their spawning main-thread.
那么如何在不手动设置每个线程的情况下为我的应用程序中的所有线程设置语言环境?
So how can I set the locale for all the threads in my application without setting it in each one manually?
推荐答案
如果您想这样做,您必须更改操作系统区域设置.您希望 BackgroundWorkers 在 en-US 中运行的原因是什么?
You'll have to change the operating system locale if you want to do that. For what reason do you want BackgroundWorkers to run in en-US?
您的业务层应该在不变的文化中运行,并且只有最终用户的 UI 具有特定的文化.
You should have your business layer running in an invariant culture, and only have a specific culture for the end user's UI.
如果您使用的是 BackgroundWorker 组件,并且必须这样做,您可以在 DoWork 方法中尝试这样的操作:
If you are using the BackgroundWorker component, and have to do this you could try something like this in the DoWork method:
// In DoWork
System.Globalization.CultureInfo before = System.Threading.Thread.CurrentThread.CurrentCulture;
try
{
System.Threading.Thread.CurrentThread.CurrentCulture =
new System.Globalization.CultureInfo("en-US");
// Proceed with specific code
}
finally
{
System.Threading.Thread.CurrentThread.CurrentUICulture = before;
}
这篇关于如何在 .Net 中更改整个进程(不仅仅是当前线程)的 CurrentCulture?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 .Net 中更改整个进程(不仅仅是当前线程)的 CurrentCulture?
基础教程推荐
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- MS Visual Studio .NET 的替代品 2022-01-01