title: How do I communicate between multiple threads?
body: <p>I'm writing a plug-in for another program which uses the native program to open a series of files to extract some data from. One problem I am having is the process takes a long time and I want to keep the user interface from hanging. Plus I also want to give the user the ability to cancel the process before it completes. In the past I've used a background worker for this type of thing, but in this case I don't think a BackgroundWorker will work.</p>
  
  <p>To create a plug-in through the API I am using one can create a custom command by inheriting from an IAPICommand interface. This interface includes an Execute(Application app) method. The class is then instantiated and the Execute() method is called by the program when the user evokes the custom command in the program. </p>
  
  <p>The Execute() method is passed a reference to the current Application object when it is called, and it is this application object that is used to open the files to extract data from. However, the application instance is not able to open a document when requested by a thread other the the original Execute() thread.</p>
  
  <p>So typically the UI would exist on the main thread, and the time consuming data extraction would be performed on a secondary thread. However, in this case the data extraction must be performed on the main thread, and I need to create a secondary thread for the UI.</p>
  
  <p>Here's a stripped down version of the code.</p>
  
  <pre><code>class MyCommand:IAPICommand
  {
      public void Execute(Application app) // method from IAPICommand
      {
          Thread threadTwo= new Thread(ShowFormMethod);
          threadTwo.Start();
      }
  
      public void ProcessWidget(Widget w, Application app)
      { 
          //uses an App to work some magic on C
          //app must be called from the original thread that called ExecuteCommand()
      }
  
      //method to open custom form on a seperatethread
      public void ShowFormMethod()
      {
        MyForm form = new MyForm();
        form.ShowDialog();  
      }
  }
  </code></pre>
  
  <p>Here is a flow chart that shows how I <em>think</em> this should ultimately work.</p>
  
  <p><a href="http://dl.dropbox.com/u/113068/SOMLibThreadingDiagram.jpg">alt text http://dl.dropbox.com/u/113068/SOMLibThreadingDiagram.jpg</a></p>
  
  <ol>
  <li>Does this diagram make any sense, and if so am I even taking the correct approach to solve this problem?</li>
  <li>Once the main thread starts the UI thread I want it to wait for the user to either select widgets to process, or end the command by closing the form (the red figures on the diagram). How can I make the main thread wait, and how do I trigger it to continue either with processing or to continue to the end when the UI thread ends? I was thinking I could have the main thread wait on a Monitor lock. The UI thread would then populate a static list of Widgets to be processed, and then pulse the main thread to trigger the processing. The UI thread would also pulse the Main thread when the form is closed, and the main thread would know to continue to the end of the command if it was ever pulsed when the list of widgets to process was empty.</li>
  <li>How do I allow the main thread to communicate the progress or completion of widget processing back to the UI thread (yellow arrows in the diagram)? Do I just used the BeginInvoke() method of the Form to do this?</li>
  <li>How do I allow the UI thread to cancel the widget processing (green arrow in the diagram)? I think I could just setup a static Boolean flag that is checked before each widget is processed?</li>
  </ol>