Wednesday, May 11, 2011

CultureInfo.CurrentCulture for New Threads

The CultureInfo.CurrentCulture for each thread is independent. When a new thread is created, the CurrentCulture is set to the default system locale information, rather than the CurrentCulture of the calling thread. The following code demonstrates this:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            CultureInfo cultureInfo = new CultureInfo("fr-FR");
            Thread.CurrentThread.CurrentCulture = cultureInfo;

            Console.WriteLine("Main Thread Culture: {0}"
                CultureInfo.CurrentCulture.DisplayName);

            Thread t = new Thread(NewThread);
            t.Start();
        }

        static void NewThread()
        {
            Console.WriteLine("New Thread Culture: {0}"
                CultureInfo.CurrentCulture.DisplayName);
        }
    }
}

The output is the following:

Main Thread Culture: French (France)
New Thread Culture: English (United States)

1 comment:

  1. Hi Mark,

    I've got a very simple bit of code on my blog that will help you set the newly created threads' culture / UI culture that you may be interested in.

    http://www.greengingerwine.com/index.php/2012/03/an-easy-way-to-set-a-newly-created-threads-culture-in-net/

    - Dylan

    ReplyDelete