未验证 提交 3081d207 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #34332 from sharwell/update-vs-threading

Update Microsoft.VisualStudio.Threading
......@@ -138,8 +138,8 @@
<MicrosoftVisualStudioTextManagerInterop120Version>12.0.30110</MicrosoftVisualStudioTextManagerInterop120Version>
<MicrosoftVisualStudioTextManagerInterop121DesignTimeVersion>12.1.30328</MicrosoftVisualStudioTextManagerInterop121DesignTimeVersion>
<MicrosoftVisualStudioTextManagerInterop160DesignTimeVersion>16.0.0</MicrosoftVisualStudioTextManagerInterop160DesignTimeVersion>
<MicrosoftVisualStudioThreadingAnalyzersVersion>15.8.192</MicrosoftVisualStudioThreadingAnalyzersVersion>
<MicrosoftVisualStudioThreadingVersion>15.8.192</MicrosoftVisualStudioThreadingVersion>
<MicrosoftVisualStudioThreadingAnalyzersVersion>16.0.102</MicrosoftVisualStudioThreadingAnalyzersVersion>
<MicrosoftVisualStudioThreadingVersion>16.0.102</MicrosoftVisualStudioThreadingVersion>
<MicrosoftVisualStudioValidationVersion>15.3.23</MicrosoftVisualStudioValidationVersion>
<MicrosoftVisualStudioVsInteractiveWindowVersion>2.0.0-rc3-61304-01</MicrosoftVisualStudioVsInteractiveWindowVersion>
<MicrosoftWin32PrimitivesVersion>4.3.0</MicrosoftWin32PrimitivesVersion>
......
......@@ -163,6 +163,7 @@ collection.
</Rules>
<Rules AnalyzerId="Microsoft.VisualStudio.Threading.Analyzers" RuleNamespace="Microsoft.VisualStudio.Threading.Analyzers">
<Rule Id="VSTHRD002" Action="None" /> <!-- Avoid problematic synchronous waits -->
<Rule Id="VSTHRD003" Action="None" /> <!-- Avoid awaiting foreign Tasks https://github.com/dotnet/roslyn/issues/34331 -->
<Rule Id="VSTHRD103" Action="None" /> <!-- Call async methods when in an async method -->
<Rule Id="VSTHRD010" Action="None" /> <!-- Invoke single-threaded types on Main thread https://github.com/dotnet/roslyn/issues/29275 -->
<Rule Id="VSTHRD110" Action="None" /> <!-- Observe result of async calls -->
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using Microsoft.VisualStudio.Threading;
namespace Microsoft.CodeAnalysis.Editor.Shared.Utilities
{
internal static class JoinableTaskFactoryExtensions
{
// Provides 'alwaysYield' support prior to https://github.com/Microsoft/vs-threading/issues/326
public static ConfiguredMainThreadAwaitable SwitchToMainThreadAsync(this JoinableTaskFactory joinableTaskFactory, bool alwaysYield, CancellationToken cancellationToken = default)
{
#pragma warning disable VSTHRD004 // Await SwitchToMainThreadAsync
return new ConfiguredMainThreadAwaitable(joinableTaskFactory.SwitchToMainThreadAsync(cancellationToken), alwaysYield);
#pragma warning restore VSTHRD004 // Await SwitchToMainThreadAsync
}
internal readonly struct ConfiguredMainThreadAwaitable
{
private readonly JoinableTaskFactory.MainThreadAwaitable _mainThreadAwaitable;
private readonly bool _alwaysYield;
public ConfiguredMainThreadAwaitable(JoinableTaskFactory.MainThreadAwaitable mainThreadAwaitable, bool alwaysYield)
{
_mainThreadAwaitable = mainThreadAwaitable;
_alwaysYield = alwaysYield;
}
public ConfiguredMainThreadAwaiter GetAwaiter()
{
return new ConfiguredMainThreadAwaiter(_mainThreadAwaitable.GetAwaiter(), _alwaysYield);
}
}
internal readonly struct ConfiguredMainThreadAwaiter : ICriticalNotifyCompletion
{
private readonly JoinableTaskFactory.MainThreadAwaiter _mainThreadAwaiter;
private readonly bool _alwaysYield;
public ConfiguredMainThreadAwaiter(JoinableTaskFactory.MainThreadAwaiter mainThreadAwaiter, bool alwaysYield)
{
_mainThreadAwaiter = mainThreadAwaiter;
_alwaysYield = alwaysYield;
}
public bool IsCompleted
{
get
{
if (_alwaysYield)
{
return false;
}
return _mainThreadAwaiter.IsCompleted;
}
}
public void OnCompleted(Action continuation)
=> _mainThreadAwaiter.OnCompleted(continuation);
public void UnsafeOnCompleted(Action continuation)
{
// This should be simplified to a simple call to UnsafeOnCompleted when vs-threading is updated to a
// version where MainThreadAwaiter implements ICriticalNotifyCompletion.
if ((object)_mainThreadAwaiter is ICriticalNotifyCompletion criticalNotifyCompletion)
{
criticalNotifyCompletion.UnsafeOnCompleted(continuation);
}
else
{
_mainThreadAwaiter.OnCompleted(continuation);
}
}
public void GetResult()
=> _mainThreadAwaiter.GetResult();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册