title: Cancel execution of command if it takes too long
body: <p>I am currently working on a piece of software that uses an assembly from a different department.<br>I call a Method from this assembly like this:</p>
  
  <pre><code>using (var connection = (IConnection) Factory.GetObject(typeof (IConnection)))
  </code></pre>
  
  <p>The code used to work perfectly fine. But for the last few minutes it seemed like my program was doing nothing when I tried to launch it. Pressing pause while debugging showed me it got "stuck" at the line above.<br>My guess is they're just doing some maintenance or something but that's not the point here.<br>
  So I though it would be nice to tell the user what went wrong if the program doesn't start. Something simple like</p>
  
  <pre><code>MessageBox.Show("Could not connect", "Connection Error");
  </code></pre>
  
  <p>And then close the program. My question is:<br>
  <strong>How do I terminate the execution of a command after a set amount of time and jump somewhere else?</strong><br>My guess would be moving it to a separate thread and then putting the calling thread to sleep for a few seconds after which it disposes of the extra thread if it isn't completed yet. But that seems <em>really</em> dirty to me and there's got to be a better way.</p>