提交 7e8e5339 编写于 作者: J Jason Malinowski

Merge pull request #7749 from jasonmalinowski/allow-joinable-task-context-as-foreground-thread

Recognize JoinableTaskSynchronizationContext as a foreground kind
......@@ -44,7 +44,9 @@ protected override void Initialize()
base.Initialize();
ForegroundThreadAffinitizedObject.CurrentForegroundThreadData = ForegroundThreadData.CreateDefault();
Debug.Assert(ForegroundThreadAffinitizedObject.CurrentForegroundThreadData.Kind == ForegroundThreadDataKind.Wpf);
Debug.Assert(
ForegroundThreadAffinitizedObject.CurrentForegroundThreadData.Kind == ForegroundThreadDataKind.Wpf ||
ForegroundThreadAffinitizedObject.CurrentForegroundThreadData.Kind == ForegroundThreadDataKind.JoinableTask);
FatalError.Handler = FailFast.OnFatalException;
FatalError.NonFatalHandler = WatsonReporter.Report;
......
......@@ -13,6 +13,7 @@ internal enum ForegroundThreadDataKind
{
Wpf,
StaUnitTest,
JoinableTask,
Unknown
}
......@@ -28,11 +29,23 @@ static ForegroundThreadDataInfo()
internal static ForegroundThreadDataKind CreateDefault()
{
var kind = SynchronizationContext.Current?.GetType().FullName == "System.Windows.Threading.DispatcherSynchronizationContext"
? ForegroundThreadDataKind.Wpf
: ForegroundThreadDataKind.Unknown;
var syncConextTypeName = SynchronizationContext.Current?.GetType().FullName;
return kind;
switch (syncConextTypeName)
{
case "System.Windows.Threading.DispatcherSynchronizationContext":
return ForegroundThreadDataKind.Wpf;
case "Microsoft.VisualStudio.Threading.JoinableTask+JoinableTaskSynchronizationContext":
return ForegroundThreadDataKind.JoinableTask;
default:
return ForegroundThreadDataKind.Unknown;
}
}
internal static ForegroundThreadDataKind CurrentForegroundThreadDataKind
......
......@@ -18,7 +18,7 @@ public static T WaitAndGetResult<T>(this Task<T> task, CancellationToken cancell
{
#if DEBUG
var threadKind = ForegroundThreadDataInfo.CurrentForegroundThreadDataKind;
if (threadKind != ForegroundThreadDataKind.Wpf && threadKind != ForegroundThreadDataKind.StaUnitTest)
if (threadKind == ForegroundThreadDataKind.Unknown)
{
// If you hit this when running tests then your code is in error. WaitAndGetResult
// should only be called from a foreground thread. There are a few ways you may
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册