Bug 46567 - Culture and UI Culture don't flow with tasks, threads, async/await as in .NET 4.6
Summary: Culture and UI Culture don't flow with tasks, threads, async/await as in .NET...
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: 4.6.0 (C8)
Hardware: All Linux
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2016-11-08 11:55 UTC by Olaf Kober
Modified: 2016-11-23 13:12 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
[HACK] Async-local Culture/UICulture (2.44 KB, patch)
2016-11-23 12:50 UTC, Damien Diederen
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Olaf Kober 2016-11-08 11:55:09 UTC
.NET Framework 4.6 introduced a feature that isn't correctly supported by Mono 4.6.1. I guess this is a bug.

Excerpt from "Changes to the task-based asynchronous pattern (TAP)" in "What's new in .NET 2015/.NET Framework 4.6" (https://msdn.microsoft.com/en-us/library/ms171868(v=vs.110).aspx#v46).

"For apps that target the .NET Framework 4.6, Task and Task<TResult> objects inherit the culture and UI culture of the calling thread. The behavior of apps that target previous versions of the .NET Framework, or that do not target a specific version of the .NET Framework, is unaffected. For more information, see the "Culture and task-based asynchronous operations” section of the CultureInfoclass topic."

"How a Thread's Culture Is Determined" in https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentculture(v=vs.110).aspx#ThreadCulture explains that in detail.

The provided example:

 public static void Main()
   {
      var tasks = new List<Task>();
      Console.WriteLine("The current culture is {0}", 
                        Thread.CurrentThread.CurrentCulture.Name);
      Thread.CurrentThread.CurrentCulture = new CultureInfo("pt-BR");
      // Change the current culture to Portuguese (Brazil).
      Console.WriteLine("Current culture changed to {0}",
                        Thread.CurrentThread.CurrentCulture.Name);
      Console.WriteLine("Application thread is thread {0}",
                        Thread.CurrentThread.ManagedThreadId);
      // Launch six tasks and display their current culture.
      for (int ctr = 0; ctr <= 5; ctr++)
         tasks.Add(Task.Run( () => {
                               Console.WriteLine("Culture of task {0} on thread {1} is {2}",
                                                 Task.CurrentId, 
                                                 Thread.CurrentThread.ManagedThreadId,
                                                 Thread.CurrentThread.CurrentCulture.Name);
                            } ));                     

      Task.WaitAll(tasks.ToArray());
   }

on Windows targeting .NET 4.6.1 outputs

    The current culture is de-AT
    Current culture changed to pt-BR
    Application thread is thread 1
    Culture of task 1 on thread 3 is pt-BR
    Culture of task 4 on thread 4 is pt-BR
    Culture of task 6 on thread 4 is pt-BR
    Culture of task 2 on thread 6 is pt-BR
    Culture of task 3 on thread 5 is pt-BR
    Culture of task 5 on thread 3 is pt-BR

on Linux with Mono 4.6.1 it outputs

    The current culture is
    Current culture changed to pt-BR
    Application thread is thread 1
    Culture of task 1 on thread 4 is
    Culture of task 2 on thread 4 is
    Culture of task 3 on thread 4 is
    Culture of task 4 on thread 4 is
    Culture of task 5 on thread 4 is
    Culture of task 6 on thread 4 is

which shows that this feature is not supported as on .NET Framework 4.6/4.6.1.
Comment 1 Damien Diederen 2016-11-23 12:50:29 UTC
Created attachment 18614 [details]
[HACK] Async-local Culture/UICulture

This is a minimal, proof-of-concept patch which reuses the async-local support from MS' implementation to implement our use-case.  I do not consider the solution "clean" by any means, but we needed a quick fix on top of mono-4.6.1.3, and this seems to fit the bill.  Uploading it cases it helps somebody else.

Also pushed to:

https://github.com/ztzg/mono/tree/x-16104-44828/culture-tasks-hack
Comment 2 Marek Safar 2016-11-23 13:12:50 UTC
Works as expected with Mono 4.8