.NET Framework 4.5 Multi-Threaded Globalization: Listing 4

Completed MainWindows.cs.

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

namespace UpdatedCultureInfoDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            UserPreferredCulture.DataContext = CultureInfo.CurrentUICulture;

            CultureInfo french = new CultureInfo("fr-FR");
            CultureInfo.DefaultThreadCurrentCulture = french;
            CultureInfo.DefaultThreadCurrentUICulture = french;

            Thread test = new Thread(new ThreadStart(NewThreadUITest));
            test.Start();

            CultureTask();

            AdditionalAppDomain();
        }

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

        private void CultureTask()
        {
            Task.Factory.StartNew(() =>
            {
                 Dispatcher.Invoke(() => TaskUICulture.DataContext = Thread.CurrentThread.CurrentUICulture);
            });
        }

        private void AdditionalAppDomain()
        {
            AppDomainSetup domainSetup = new AppDomainSetup();
            domainSetup.AppDomainInitializer = (cultures) =>
            {
                CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CreateSpecificCulture(cultures[0]);
                CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture(cultures[0]);
            };
            domainSetup.AppDomainInitializerArguments = new string[] { "es-ES" };

            AppDomain appDomain = AppDomain.CreateDomain("TestDomain", null, domainSetup);
            AppDomainCultureData appDomainData = (AppDomainCultureData)appDomain.CreateInstanceAndUnwrap
            (typeof(AppDomainCultureData).Assembly.FullName,
                typeof(AppDomainCultureData).FullName);

            Dispatcher.Invoke(() => AppDomainUICulture.DataContext = appDomainData.GetCultureInfo());
        }
    }

    public class AppDomainCultureData : MarshalByRefObject
    {
        public CultureInfo GetCultureInfo()
        {
            return CultureInfo.CurrentUICulture;
        }
    }
}

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

  • Full-Stack with a Side of Copilot: Building and Deploying an App the AI-Accelerated Way

    In this Q&A, developer and VSLive! speaker Esteban Garcia explains how GitHub Copilot can accelerate the full software development lifecycle -- from architecture and code to tests, CI/CD, and Azure deployment -- and how to use it as a repeatable engineering workflow rather than just a faster autocomplete tool.

  • VS Code 1.127 Further Integrates Advanced Browser-AI Tech

    Microsoft's July 1 Visual Studio Code update continues a recent push to make the editor's integrated browser a more capable development surface -- and a more useful tool for AI agents.

  • Support Vector Regression with SGD Training Using C#

    Support vector regression can predict numeric values effectively, and this article shows how to implement and train a kernel SVR model in C# using stochastic sub-gradient descent.

  • New GitHub Switch Limits Repo Issue Creation to Collaborators Only

    After publicly touting pull request limits as a way to cut maintainer noise, GitHub is taking the same idea further with a new setting that lets repository admins restrict issue creation to collaborators only.

Subscribe on YouTube