VSTHRD010 warning despite call to JoinableTaskFactory.SwitchToMainThreadAsync
Bug description
I pushed the threading version for a certain project. Now, the warning VSTHRD010 is coming up for certain parts of code despite the call to JoinableTaskFactory.SwitchToMainnThreadAsync.
Repro steps
public static async Task<bool> TestMethod(JoinableTaskFactory jtf)
{
await jtf.SwitchToMainThreadAsync(VsShellUtilities.ShutdownToken);
#pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread
// do something that requires main thread
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread
}
Expected behavior
No warning should appear.
Actual behavior
Warning appeared, and we have to suppress the warning. Despite switching to main thread.
- Version used: 17.10.41
I agree this shouldn't be happening. Your sample code is about as simple as it gets for what should work however. In fact it's equivalent to this test we have:
https://github.com/microsoft/vs-threading/blob/3803a7867ed55df2ef82db4544a117b7fd2b4289/test/Microsoft.VisualStudio.Threading.Analyzers.Tests/VSTHRD010MainThreadUsageAnalyzerTests.cs#L738-L742
So I can only imagine there is something else about your repro that is important to understand why it's broken. Any ideas what that might be?
17.12.19 no longer repros this, in a case that we had internally that seemed to repro the same issue.