Example #1
0
    public static void Main()
    {
        string formatString = "C2";

        Console.WriteLine("The example is running on thread {0}",
                          Thread.CurrentThread.ManagedThreadId);
        // Make the current culture different from the system culture.
        Console.WriteLine("The current culture is {0}",
                          CultureInfo.CurrentCulture.Name);
        if (CultureInfo.CurrentCulture.Name == "fr-FR")
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        }
        else
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
        }

        Console.WriteLine("Changed the current culture to {0}.\n",
                          CultureInfo.CurrentCulture.Name);

        // Call an async delegate to format the values using one format string.
        Console.WriteLine("Executing a task asynchronously in the main appdomain:");
        var t1 = Task.Run(() => { DataRetriever d = new DataRetriever();
                                  return(d.GetFormattedNumber(formatString)); });

        Console.WriteLine(t1.Result);
        Console.WriteLine();

        Console.WriteLine("Executing a task synchronously in two appdomains:");
        var t2 = Task.Run(() => { Console.WriteLine("Thread {0} is running in app domain '{1}'",
                                                    Thread.CurrentThread.ManagedThreadId,
                                                    AppDomain.CurrentDomain.FriendlyName);
                                  AppDomain domain = AppDomain.CreateDomain("Domain2");
                                  DataRetriever d  = (DataRetriever)domain.CreateInstanceAndUnwrap(typeof(Example).Assembly.FullName,
                                                                                                   "DataRetriever");
                                  return(d.GetFormattedNumber(formatString)); });

        Console.WriteLine(t2.Result);
    }