Microsoft changed VSIX to asynchronous with VS2019 and with that came grief.
I see many warnings that say:
Warning VSTHRD010 Accessing "Project" should only be done on the main thread. Call Microsoft.VisualStudio.ProjectSystem.IProjectThreadingService.VerifyOnUIThread() first.
I did add, though thinking of removing, the Nuget package Microsoft.VisualStudio.ProjectSystem
, not that that helped.
Adding that line to the top of a method yields:
Error CS0120 An object reference is required for the non-static field, method, or property 'IProjectThreadingService.VerifyOnUIThread()'
A search was not helpful to resolve this last error.
I tried adding:
`Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
but that did not change things.
Thoughts?
UPDATE
I presume the close vote was from the user that said to give code. There is no "code". Anyone who has done an upgrade from prior to VS2019 to VS2019 and ran into the synchronous VSIX to asynchronous VSIX will understand the issue. One cannot just use items like before. There are many of these warnings splattered because VS now classifies many elements as "Main UI Accessible" only. That means
if (null == oItems || 0 == oItems.Count)
return false;
no longer works. oItems is defined as EnvDTE.ProjectItems. Hence the warning
Accessing ProjectItems. Basically, anything in
EnvDTE` is off limits.
The code will be anything that touches/uses EnvDTE objects.
RESPONSE FOR ANSWER 1
I implemented answer and got thrown a new error.
private void MenuItemCallback(object sender, EventArgs e)
{
info VSTHRD102 -> this.packageVsi.JoinableTaskFactory.Run(async () =>
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
this.EventOptionsDialog();
});
}
private Task EventOptionsDialog()
{
_ = System.Threading.Tasks.Task.Run(async () =>
{
await this.packageVsi.DoTask1Async();
}
);
// Other tasks
return null;
}
The informational message that comes back is:
Severity VSTHRD102 Limit use of synchronously blocking method calls such as JoinableTaskFactory.Run or Task.Result to public entrypoint members where you must be synchronous. Using it for internal members can needlessly add synchronous frames between asynchronous frames, leading to threadpool exhaustion.
- I want clean working code, so no messages, warnings, or errors.
- There is a heck of a lot of code that needs a solution, so if this technique exhausts the thread pool, then this answer is not good.
- There is a lot of downstream methods and from what I observed, VS2019 is stupid and does not know that a sub-method already has the main UI, though. The process complicates a bit, as some methods get called from multiple locations.
- I still want clean code without having many new code blocks added.