Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <code>&lt;STAThread()&gt;</code> attribute cannot be added to classes like your form. It <em>only</em> works when it is applied to the <code>Main</code> function, which is the entry point of your application.</p> <p>But VB.NET hides this function from you because it is rare that one needs to mess with <code>Main</code> in a WinForms application. It is just needed to get the plumbing set up for your app, which the compiler can manage for you. This is controlled by the "Application Framework" checkbox in the project options. If this is checked, the compiler automatically generates the <code>Main</code> function and the required plumbing. You can disable this option, but it makes life quite a bit harder for the average WinForms developer because you'll have to write and maintain your own <code>Main</code> function.</p> <p>The real question here is why this is a problem at all. The compiler-generated <code>Main</code> function for a WinForms application is always going to have the <code>STAThread</code> attribute applied to it. That is just how the WinForms framework is designed to run. If that is not happening, then there is something <em>badly</em> wrong with your project. I would recommend scrapping it and starting over letting Visual Studio create a new WinForms project from one of the built-in templates. Everything should Just Work™.</p> <p>The other option, of course, is that you're trying to display the <code>OpenFileDialog</code> on a separate thread (other than your main UI thread). But from your description in the question (adding a button to the form to display the dialog), it doesn't sound like this is the case. Regardless, the solution is not to do that. For example, if you're using a <code>BackgroundWorker</code> to do work on a non-UI thread in order to keep the UI responsive, that's great, but you'll want to do all of the UI stuff like showing an <code>OpenFileDialog</code> on the main UI thread <em>before</em> invoking the <code>BackgroundWorker</code>. There <em>is</em> a way to set a particular thread's apartment state using the <code>SetApartmentState</code> function, but I really don't recommend showing an <code>OpenFileDialog</code> on a background thread.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload