.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

  • GitLab Takes Over VS Code Extension, Plans Improvements

    DevOps specialist GitLab has officially taken over the control of a GitLab extension for Microsoft's open source, cross-platform Visual Studio Code editor.

  • VS Code Python Tool Now Does Native Notebooks

    The Python Extension for VS Code Insiders team is previewing the newest implementation of notebooks, used frequently in data science with offerings such as Jupyter Notebooks.

  • As .NET 5 Nears, Content/Documentation Reorganization Starts

    A GitHub project is seeking to reorganize documentation and developer content in advance of the November debut of .NET 5, a unification of all things .NET that combines. .NET Core and other components.

  • Windows Devs Get Cross-Platform Page, Issues Repo

    Developers doing their coding on the Windows OS have received two new resource gifts from Microsoft: a new landing page for those using cross-platform technologies and a new GitHub repo with which to report issues to Windows engineering teams.Developers doing their coding on the Windows OS have received two new resource gifts from Microsoft: a new landing page for those using cross-platform technologies and a new GitHub repo with which to report issues to Windows engineering teams.

  • What's New in Entity Framework Core 5.0 Preview 7

    Microsoft shipped the seventh preview of Entity Framework Core 5.0, boosting its data access technology with a factory to create DbContext instances and more.

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events