You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JoinableTaskContext.IsOnMainThread should satisfy VSTHRD010 #1100
I have a method I want to be free-threaded, but I have dependencies which are not. How I implement my method depends whether it's running on the main thread, or a background thread. Copying the example from the other issue:
publicvoidNotAffinitized(){if(IsOnMainThread()){// This incorrectly marks the enclosing method as affinitized, which then propagatesSomeAffinitizedMethod();}else{SomeNonAffinitizedMethod();}}
Describe the solution you'd like
No VSTHRD010 within the if block.
Describe alternatives you've considered
An await jtf.SwitchToMainThreadAsync(alwaysYield: false) does suppress the warning, but feels inefficient. Even if it's fast, it's still extra work that isn't necessary for program correctness, just to satisfy the analyzer.
Here's some test code that I believe should be made to work:
vartest=@"using System.Threading.Tasks;using Microsoft.VisualStudio.Shell;using Microsoft.VisualStudio.Shell.Interop;using Task = System.Threading.Tasks.Task;class Test{ void Foo() { ThreadHelper.ThrowIfNotOnUIThread(); IVsSolution solution = Package.GetGlobalService(typeof(SVsSolution)) as IVsSolution; } async Task FirstAsync() { if (ThreadHelper.JoinableTaskFactory.Context.IsOnMainThread) { Foo(); // this generates a warning, even though Reset() doesn't assert } }}";awaitCSVerify.VerifyAnalyzerAsync(test);
However, I haven't yet been able to get the product code to make the test pass. I don't have any experience with the Roslyn APIs, so it's a learning curve to contribute. As much as I'd love to get my own name in the commit history, if someone else can get this implemented before me, I'll be happy to be able to take advantage of the improvement.
Activity
zivkan commentedon Oct 29, 2022
Here's some test code that I believe should be made to work:
However, I haven't yet been able to get the product code to make the test pass. I don't have any experience with the Roslyn APIs, so it's a learning curve to contribute. As much as I'd love to get my own name in the commit history, if someone else can get this implemented before me, I'll be happy to be able to take advantage of the improvement.