.NET Framework 4.5 Multi-Threaded Globalization: Listing 1

The .NET Framework 4 multi-threading culture setting sample.

private void CultureTask(CultureInfo culture)
{
    Task.Factory.StartNew((c) =>
        {
          Dispatcher.Invoke(() => TaskUICulture.DataContext = c);
        }, culture);
    
}

using System.Windows;
using System.Threading;
using System.Globalization;
using System.Threading.Tasks;
using System;
using System.Windows.Threading;

namespace VSMCultureInfoDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            UserPreferredCulture.DataContext = CultureInfo.CurrentUICulture;

            // Change culture of the current thread
            CultureInfo french = new CultureInfo("fr-Fr");
            Thread.CurrentThread.CurrentCulture = french;
            Thread.CurrentThread.CurrentUICulture = french;

            Thread cultureParameterized = new Thread(new ParameterizedThreadStart(SetThreadCulture));
            cultureParameterized.Start(french);

            Thread cultureAfter = new Thread(new ThreadStart(CultureAfterTest));
            cultureAfter.CurrentCulture = Thread.CurrentThread.CurrentUICulture;
            cultureAfter.CurrentUICulture = Thread.CurrentThread.CurrentUICulture;
            cultureAfter.Start();

            CultureTask(Thread.CurrentThread.CurrentUICulture);
        }

        private void CultureAfterTest()
        {
            Dispatcher.Invoke(() => ThreadUIPropCulture.DataContext = Thread.CurrentThread.CurrentUICulture);
        }

        private void SetThreadCulture(object culture)
        {
            CultureInfo cultureInfo = culture as CultureInfo;
            Thread.CurrentThread.CurrentCulture = cultureInfo;
            Thread.CurrentThread.CurrentUICulture = cultureInfo;

            Dispatcher.Invoke(() => ThreadUIParamCulture.DataContext = Thread.CurrentThread.CurrentUICulture);
        }

        private void CultureTask(CultureInfo culture)
        {
            Task.Factory.StartNew((c) =>
                {
                  Dispatcher.Invoke(() => TaskUICulture.DataContext = c);
                }, culture);
            
        }
    }
}

About the Author

Eric Vogel is a Senior Software Developer for Red Cedar Solutions Group in Okemos, Michigan. He is the president of the Greater Lansing User Group for .NET. Eric enjoys learning about software architecture and craftsmanship, and is always looking for ways to create more robust and testable applications. Contact him at [email protected].

comments powered by Disqus

Featured

  • Python in VS Code Adds Data Viewer for Debugging

    The January 2021 update to the Python Extension for Visual Studio Code is out with a short list of new features headed by a data viewer used while debugging.

  • GitHub Ships Enterprise Server 3.0 Release Candidate

    It's described as "the biggest ever change to Enterprise Server," with improvements to Actions, Packages, mobile, security and more.

  • Attacks on .NET Apps Grow in Number, Severity, Says Security Firm

    .NET apps were found to have more serious vulnerabilities and suffer more attacks last year, according to data gathered by Contrast Labs.

  • Microsoft Opens Up Old Win32 APIs to C# and Rust, More Languages to Come

    Microsoft is opening up old Win32 APIs long used for 32-bit Windows programming, letting coders use languages of their choice instead of the default C/C++ option.

Upcoming Events