title: VB.Net: Understanding the way Application.Run() works
body: <p>Hans Passant gave me a great answer <a href="https://stackoverflow.com/questions/4062755">here</a>, so I thought of asking for more details to try to understand the way <code>Application.Run()</code> works.</p>
  
  <p>As far as I understand from the docs, it seems that <code>Application.Run()</code> starts a message loop on the current thread, which in turns enables it to process user input (Is that right?). The overloaded version  <code>Application.Run(Form)</code> basically does the same, only it exists when the form closes, and it shows the form by default.</p>
  
  <p>That raises a few questions:</p>
  
  <p>
  <li>How would one do to simply call from the <code>Main()</code> sub a function that can communicate with the user to (message boxes and so on) and wait for it to exit?</li></p>
  
  <p><li>When the message loop is started without a form, how do you launch a new form from this loop, and wait for it to exit? <code>ShowDialog</code> could work, unless you don't want the form to display immediately when launched (eg. if you have a for that's launched minimized to the system tray)</li></p>
  
  <ul>
  <li>Basically, the situation would be as follows: sub `Main` has a list of tasks to execute in 20mn, with a system tray icon telling the user that the program will operate in 20mn. A timer ticks after 20mns, and has to execute say approx. 15 tasks one by one, every time creating an instance of a progress dialog, initially hidden in the taskbar.</li>
  <li>`ShowDialog` would display the form, which is not wanted; so the way I would do it would be to pass the progress dialog a callback to a function that starts the next task. But that wouldn't exit the first progress form before the second has exited, would it? Which means 15 forms would end up being opened...</li>
  <li>So the solution may be to invoke (begininvoke?) the callback on the main application loop... Only, I don't know how to do this, because I don't have a form associated with the loop to invoke the callback on...</li> 
  </ul>
  
  <p>I hope my questions are clear (I might confuse many things, sorry),<br>
  Thanks,<br>
  CFP.</p>