用于设置 CurrentCulture 的线程创建事件
我们的应用程序允许用户更改其运行所在的文化,并且该文化可以与底层操作系统文化不同。 我发现执行此操作的唯一方法是为每个线程设置 Thread.CurrentThread.CurrentCulture 和 Thread.CurrentThread.CurrentUICulture。
唯一的问题是我们必须为每个线程设置区域性,并且每当我们创建新线程时都需要记住执行此操作。 如果将线程设置为另一种区域性,然后创建一个新线程,它将获取操作系统的区域性,而不是创建它的线程的区域性。
我想找到一种更好的方法来做到这一点。 我只能想到两件事,但我不知道是否可能。
- 在应用程序级别设置区域性,以便所有线程都默认为该区域性。 这可能吗?
- 是否有我可以订阅的线程创建事件? 这样我就可以设置一个处理程序来设置线程创建的区域性。
即使我需要 PInvoke 到 Win32 API,任何想法或帮助都会受到欢迎。 提前致谢。
编辑:我发现这个问题< /a> 类似,但没有找到答案。
Our application allows the user to change the Culture that they run it under and that culture can be different than the underlying OS Culture. The only way that I have found of doing this is by setting Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture for every thread.
The only problem with this is that we must set the culture for every thread and we need to remember to do this whenever we create a new thread. If you set a thread to another culture, then create a new thread, it gets the culture of the OS, not of the thread that created it.
I would like to find a better way of doing this. I can only think of two things, but I don't know if either are possible.
- Set the culture at the application level so that all threads default to that culture. Is this possible?
- Is there a thread creation event anywhere that I can subscribe to? This way I can set up one handler to set the culture on thread creation.
Any ideas or help would be welcome, even if I need to PInvoke down to the Win32 API. Thanks in advance.
EDIT: I found this question which is similar, but no answer was found.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可能不是确切的解决方案,但是创建一个负责线程创建的类怎么样?
用法:
May not be the exact solution, but how about creating a class that responsible for your thread creation ?
Usage:
文化是 Windows 中特定于线程的概念,Windows 中的线程实际上与进程或应用程序域无关。 线程和保存文化信息(据我所知)的操作系统之间没有抽象级别,因此您必须在每个线程上设置它。
Culture is a thread-specific concept in Windows, and threads in Windows are actually agnostic of a process or appdomain. There is no level of abstraction between a thread and the OS that holds culture info (that I know of), so you have to set it on each thread.
.NET 4.5 中的 CultureInfo 类添加了两个新属性来解决此问题,DefaultThreadCurrentCulture 和 DefaultThreadCurrentUICulture< /a>.
现在您可以设置这两个属性,所有新线程都将设置为默认区域性而不是系统区域性。 这些属性还将为所有未明确设置其区域性的现有线程设置区域性。
Two new properties were added to the CultureInfo class in .NET 4.5 which solve this problem, DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture.
Now you can set these two properties and all new threads will be set to the default culture instead of the system culture. These properties will also set the culture for all existing threads that do no explicitly have their culture set.
只是为了添加@Rob Prouse 的答案。 这是一个快速演示。
设置应用程序初始化的文化:
在我的 .ToString() 上使用区域性信息。
Just to add to @Rob Prouse answer. Here is a quick demonstration.
Setting the culture on App intialization :
Using the culture info on my .ToString().