title: How to handle starting/stopping API interface that may take a long time to start
body: <p>I have an application that needs to work with a vendor-supplied API to do Screen Pops when a call is routed to the user's extension.  Another developer worked on getting the API to work and delivered a prototype wrapper class that isn't quite right, and I'm trying to get it right.</p>
  
  <p>Because this API can block for several minutes before returning, the developer started the API interface code in a separate thread, like this:</p>
  
  <pre><code>// constructor
  public ScreenPop( ...params... )
  {
    ...
  t = new Thread( StartInBackground );
  t.Start();
  )
  
  private void StartInBackground()
  {
    _listener = new ScreenPopTelephoneListener();
    _bAllocated = true;
    _listener.Initialize( _address );
    _bInitialized = true;
    _listener.StartListening( _extension );
    _bListening = true;
  }
  </code></pre>
  
  <p>The call to Initialize is the one that could hang, if the phone server was unresponsive.</p>
  
  <p>I don't think it was intentional on the part of the developer who did this, but when the API is initialized this way, the thread keeps running, so that now I have to stop the thread before I can clean up when I want to.  I also have to use Invoke to get events from the thread, as others so kindly pointed out in my previous question, but that is working now.</p>
  
  <p>Anyway, here is the logic used to shut everything down and clean up:</p>
  
  <pre><code>public void ShutdownFunc()
  {
    try
    {
      if ( _bListening )
      {
        _listener.StopListening();
        _bListening = false;
      }
      if ( _bInitialized )
      {
        _listener.Shutdown();
        _bInitialized = false;
      }
      if ( _bAllocated )
      {
        _listener = null;
        _bAllocated = false;
      }
    }
  }
  </code></pre>
  
  <p>Of course, there is a try/catch around the whole mess to prevent unhandled exceptions.</p>
  
  <p>My biggest problem is, how do I call the shutdown code when the API interface code is running in a separate thread?  I need to clean up in case the user logs off and then logs back on again, as the API gets confused if I don't.  Thread.Abort won't work, as it's not in a place to catch the exception, and the "volatile bool" method won't work either, for obvious reasons (most of the time, the API thread is not active).</p>
  
  <p>So there seems to be no way to call the shutdown logic if the API interface code is running on a separate thread.  Thread.Abort() bypasses all the logic and kills the thread without cleaning up.  Calling the shutdown logic from the GUI thread hangs when it calls the Shutdown method.  So, what can I do?</p>
  
  <p>The IDispose interface just adds another layer of abstraction in the GUI thread, and does nothing to solve the problem here.</p>
  
  <p>For now, to get the rest of the code developed, I am eliminating threads completely.  But, before I release the app, I need to figure this out.</p>
  
  <p>Anybody?</p>
  
  <p>Thanks,<br>
  Dave</p>