Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 1e966cc

Browse files
committed
Merge pull request #4154 from tarekgh/port-cultureinfo-rc2
Fix Current Culture to make it travel in async operations
2 parents ab627c3 + f8c3b96 commit 1e966cc

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/mscorlib/corefx/System/Globalization/CultureInfo.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ public partial class CultureInfo : IFormatProvider
133133
//The parent culture.
134134
private CultureInfo m_parent;
135135

136+
static AsyncLocal<CultureInfo> s_asyncLocalCurrentCulture;
137+
static AsyncLocal<CultureInfo> s_asyncLocalCurrentUICulture;
138+
139+
static void AsyncLocalSetCurrentCulture(AsyncLocalValueChangedArgs<CultureInfo> args)
140+
{
141+
s_currentThreadCulture = args.CurrentValue;
142+
}
143+
144+
static void AsyncLocalSetCurrentUICulture(AsyncLocalValueChangedArgs<CultureInfo> args)
145+
{
146+
s_currentThreadUICulture = args.CurrentValue;
147+
}
148+
136149
//
137150
// The CultureData instance that reads the data provided by our CultureData class.
138151
//
@@ -311,7 +324,13 @@ public static CultureInfo CurrentCulture
311324
{
312325
throw new ArgumentNullException("value");
313326
}
314-
s_currentThreadCulture = value;
327+
328+
if (s_asyncLocalCurrentCulture == null)
329+
{
330+
Interlocked.CompareExchange(ref s_asyncLocalCurrentCulture, new AsyncLocal<CultureInfo>(AsyncLocalSetCurrentCulture), null);
331+
}
332+
// this one will set s_currentThreadCulture too
333+
s_asyncLocalCurrentCulture.Value = value;
315334
}
316335
}
317336

@@ -355,8 +374,14 @@ public static CultureInfo CurrentUICulture
355374
}
356375

357376
CultureInfo.VerifyCultureName(value, true);
377+
378+
if (s_asyncLocalCurrentUICulture == null)
379+
{
380+
Interlocked.CompareExchange(ref s_asyncLocalCurrentUICulture, new AsyncLocal<CultureInfo>(AsyncLocalSetCurrentUICulture), null);
381+
}
358382

359-
s_currentThreadUICulture = value;
383+
// this one will set s_currentThreadUICulture too
384+
s_asyncLocalCurrentUICulture.Value = value;
360385
}
361386
}
362387

0 commit comments

Comments
 (0)