title: VSTHRD010: Accessing item should only be done on the main thread
body: <p>Microsoft changed VSIX to asynchronous with VS2019 and with that came grief.</p>
  
  <p>I see many warnings that say:</p>
  
  <blockquote>
    <p>Warning   VSTHRD010   Accessing "Project" should only be done on the main thread. Call Microsoft.VisualStudio.ProjectSystem.IProjectThreadingService.VerifyOnUIThread() first.</p>
  </blockquote>
  
  <p>I did add, though thinking of removing, the Nuget package <code>Microsoft.VisualStudio.ProjectSystem</code>, not that that helped.</p>
  
  <p>Adding that line to the top of a method yields:</p>
  
  <blockquote>
    <p>Error CS0120  An object reference is required for the non-static field, method, or property 'IProjectThreadingService.VerifyOnUIThread()'</p>
  </blockquote>
  
  <p>A search was not helpful to resolve this last error.</p>
  
  <p>I tried adding:</p>
  
  <blockquote>
    <p>`Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();</p>
  </blockquote>
  
  <p>but that did not change things.</p>
  
  <p>Thoughts?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>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</p>
  
  <pre><code>if (null == oItems || 0 == oItems.Count)
     return false;
  </code></pre>
  
  <p>no longer works. oItems is defined as EnvDTE.ProjectItems<code>. Hence the warning</code>Accessing ProjectItems<code>. Basically, anything in</code>EnvDTE` is off limits.</p>
  
  <p>The code will be anything that touches/uses EnvDTE objects.</p>
  
  <p><strong>RESPONSE FOR ANSWER 1</strong></p>
  
  <p>I implemented answer and got thrown a new error.</p>
  
  <pre><code>private void MenuItemCallback(object sender, EventArgs e)
  {
  info VSTHRD102 -&gt; this.packageVsi.JoinableTaskFactory.Run(async () =&gt;
      {
          await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
          this.EventOptionsDialog();
      });
  }
  
  private Task EventOptionsDialog()
  {
      _ = System.Threading.Tasks.Task.Run(async () =&gt;
      {
          await this.packageVsi.DoTask1Async();
      }
      );
  
      // Other tasks
  
      return null;
  }
  </code></pre>
  
  <p>The informational message that comes back is:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <ol>
  <li>I want clean working code, so no messages, warnings, or errors.</li>
  <li>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.</li>
  <li>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.</li>
  <li>I still want clean code without having many new code blocks added.</li>
  </ol>