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
VSTHRD003 False positive in top-level statements #1370
The VSTHRD003 is triggered when using a combination of Task.Run and Task.WhenAny in a "top-level statements" (programs without Main methods) console app
Repro steps
This "top-level statements" code will trigger VSTHRD003
usingvarcts=newCancellationTokenSource();varloopTask=Task.Run(async()=>{// DO SOMETHING...},cts.Token);varserverTask=Task.Run(async()=>{// DO SOMETHING...},cts.Token);varexitTask=Task.Run(()=>{Console.WriteLine("Press any key to exit");Console.ReadKey();cts.Cancel();},cts.Token);awaitTask.WhenAny(loopTask,serverTask,exitTask);/// VSTHRD003 is triggered here !Console.WriteLine("Exited");
This traditional console app, on the other hand, does not trigger VSTHRD003
publicstaticclassProgram{publicstaticasyncTaskMain(string[]args){usingvarcts=newCancellationTokenSource();varloopTask=Task.Run(async()=>{// DO SOMETHING...},cts.Token);varserverTask=Task.Run(async()=>{// DO SOMETHING...},cts.Token);varexitTask=Task.Run(()=>{Console.WriteLine("Press any key to exit");Console.ReadKey();cts.Cancel();},cts.Token);awaitTask.WhenAny(loopTask,serverTask,exitTask);/// VSTHRD003 is NOT triggered here !Console.WriteLine("Exited");}}
Expected behavior
VSTHRD003 should not be triggered in this scenario
Actual behavior
VSTHRD003 is triggered in this scenario, and it shouldn't
Activity