vs-threading icon indicating copy to clipboard operation
vs-threading copied to clipboard

VSTHRD010 warning despite call to JoinableTaskFactory.SwitchToMainThreadAsync

Open kfam98 opened this issue 1 year ago • 1 comments

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

kfam98 avatar Mar 29 '24 00:03 kfam98

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?

AArnott avatar Apr 06 '24 00:04 AArnott

17.12.19 no longer repros this, in a case that we had internally that seemed to repro the same issue.

AArnott avatar Sep 27 '24 16:09 AArnott