是否可以为 .NET 应用程序或只是一个线程设置 CultureInfo?

发布于 2024-08-21 01:30:33 字数 916 浏览 4 评论 0原文

我有一个用 C# 编写的应用程序,它没有 GUI 或 UI,而是编写由另一个应用程序(XML 等)解析的文件。

我有一位客户的 CultureInfo 将 NumberDecimalSeparator 设置为逗号,这会导致浮点数解析错误(PI 最终为 3,1415)。

我想要一种在应用程序中为所有线程全局设置 CultureInfo 的方法。我尝试过:

  1. CurrentThread.CurrentCulture 设置为 Main() 中的第一行(显然)的习惯方法,但它似乎被重置了。
  2. http://www.codeproject.com/KB/cs/Change_App_Culture 上的变体/扩展。 aspx
  3. 对应用程序中显式创建的线程执行相同的操作 (#1)。

并且更改为使用显式格式也不是一个选择(150K+ 行,大部分由前员工编写)。

[编辑] 该应用程序绑定到套接字并处理来自专用客户端的请求。根据请求类型,它会生成不同的处理程序类。

抱歉,当我第一次发布时,我应该在 #1 中澄清(我虽然)我也在所有显式生成的处理程序中执行了此操作。

事实证明我错过了导致问题的线程/处理程序。因此应用程序现在可以正常工作,但问题仍然是是否可以在所有线程上设置区域性。

如果它可以迭代所有线程,也可以解决问题。那么:

如何获取当前进程中的所有Thread对象(而不是ProcessThread)?

I've an application written in C# which has no GUI or UI, but instead writes files that are parsed by another application (in XML and others).

I have a customer whose CultureInfo has the NumberDecimalSeparator set to a comma, which causes parsing errors with floating point numbers (PI would end up as 3,1415).

I'd like a way to set the CultureInfo globally within the application, for all threads. I've tried:

  1. The (apparently) customary approach of setting CurrentThread.CurrentCulture as the first line in Main() but it appears to get reset.
  2. A variation/expansion on http://www.codeproject.com/KB/cs/Change_App_Culture.aspx
  3. Do the same (#1) on the explicitly created threads in the application.

And changing to use explicit formatting is not an option (150K+ lines, most written by former employees).

[Edit]
The application binds to a socket and handles requests from dedicated clients. Depending on the request type it spawns different handler classes.

Sorry, when I first posted I should have clarified in #1 that (I though) I had done this in all of the handlers that were explicitly spawned, too.

It turns out that I missed the thread/handler that was causing the problem. So the application is working properly now, but the question remains about if the culture can be set on all threads.

If it could iterate over all threads, it would solve the issue, too. So:

How can I get all Thread objects (not ProcessThread) in the current process?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

林空鹿饮溪 2024-08-28 01:30:33

在 .NET 4.5 中,您可以使用 CultureInfo.DefaultThreadCurrentCulture

In .NET 4.5 you can use CultureInfo.DefaultThreadCurrentCulture

记忆消瘦 2024-08-28 01:30:33

不幸的是,每个新线程都以系统区域设置信息启动,即使它是从其区域设置更改为其他内容的线程启动的。

这是我在使用 BackgroundWorker 加载文件时在我们的一个应用程序中遇到的一个巨大问题。

我成功使用的方法是在启动线程上设置区域设置,然后使用线程工厂创建具有“应用程序区域设置”的线程。对于BackgroundWorkers,您可以使用工厂或派生类,因为Thread 是密封的,而BackgroundWorker 则不是。

Unfortunately, every new thread starts with system locale information, even if it's started from a thread that's had its locale changed to something else.

This was a huge gotcha I ran into in one of our apps when using a BackgroundWorker to load a file.

The approach I've used successfully is to set the locale on the startup thread, and then use a thread factory to create threads with the "application locale." For BackgroundWorkers you can use either a factory or a derived class, as Thread is sealed while BackgroundWorker is not.

幻想少年梦 2024-08-28 01:30:33

我认为您无法为整个应用程序设置区域性,但您可以在创建线程时设置区域性显式:

using System;
using System.Globalization;
using System.Threading;

class Program {

    static void thread_test() {
        Console.WriteLine("Culture: {0}", CultureInfo.CurrentCulture.DisplayName);
    }

    public static void Main(params string[] args) {
        Thread t = new Thread(thread_test);
        t.CurrentCulture = new CultureInfo("it-it");
        t.Start();
        t.Join();
    }
}

I don't think you can set the culture for the whole application, but you can set the culture explicilty whenever you create a thread:

using System;
using System.Globalization;
using System.Threading;

class Program {

    static void thread_test() {
        Console.WriteLine("Culture: {0}", CultureInfo.CurrentCulture.DisplayName);
    }

    public static void Main(params string[] args) {
        Thread t = new Thread(thread_test);
        t.CurrentCulture = new CultureInfo("it-it");
        t.Start();
        t.Join();
    }
}
赢得她心 2024-08-28 01:30:33

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文