title: Implementing timeout for calling generic function with parameters
body: <p>I am trying to get my head around the use of the Action delegate type for use in forcing a timeout when methods called in a 3rd party COM dll hang up. After much searching I find that I can use Action&lt;> or Func&lt;> and pass up to 4 generic parameters depending on whether the method called returns a parameter or not.</p>
  
  <p>For this instance I wish to call a timeout on a series of methods that return void and take 2 parameters. What follows is the code that I am putting together, but I am unable to determine how to correctly code the BeginInvoke, I am prompted to place "T arg1" and "T arg2" but when I enter param1 or param2 VS2008 tells me that these values are indeterminate.</p>
  
  <p>Here is the code as it is so far:</p>
  
  <pre><code>static void CallAndWait(Action&lt;T, T&gt; action, int timeout)
  {
      Thread subThread = null;
      Action&lt;T, T&gt; wrappedAction = (param1, param2) =&gt;
      {
          subThread = Thread.CurrentThread;
          action(param1, param2);
      };
  
      IAsyncResult result = wrappedAction.BeginInvoke(param1, param2, null, null);
      if (((timeout != -1) &amp;&amp; !result.IsCompleted) &amp;&amp;
      (!result.AsyncWaitHandle.WaitOne(timeout, false) || !result.IsCompleted))
      {
          if (subThread != null)
          {
              subThread.Abort();
          }
  
          //TODO: close external resource.
  
          throw new TimeoutException();
      }
      else
      {
          action.EndInvoke(result);
      }
  }
  </code></pre>
  
  <p>Any ideas on what is wrong here would be much appreciated.</p>
  
  <p><strong>Below is a re-edited code based on the first comment</strong></p>
  
  <p>Thanks for the input so far. The following compiles. I just can't seem to get the syntax right in calling it.</p>
  
  <pre><code>public static void CallAndWait&lt;T1, T2&gt;(Action&lt;T1, T2&gt; action, int timeout)
  {
      Thread subThread = null;
      T1 param1 = default(T1);
      T2 param2 = default(T2);
  
      Action&lt;T1, T2&gt; wrappedAction = (p1, p2) =&gt;
      {
          subThread = Thread.CurrentThread;
          action(param1, param2);
      };
  
      IAsyncResult result = wrappedAction.BeginInvoke(param1, param2, null, null);
      if (((timeout != -1) &amp;&amp; !result.IsCompleted) &amp;&amp;
      (!result.AsyncWaitHandle.WaitOne(timeout, false) || !result.IsCompleted))
      {
          if (subThread != null)
          {
              subThread.Abort();
          }
  
          //TODO: close external resource.
  
          throw new TimeoutException();
      }
      else
      {
          action.EndInvoke(result);
      }
  }
  </code></pre>
  
  <p>I am trying to test this by calling the following method with it:</p>
  
  <pre><code>public void LongTimeProcess(int a, string b)
  {
      Thread.Sleep(a);
  }
  </code></pre>
  
  <p>But the following code is not correct:</p>
  
  <pre><code>    Action&lt;int, string&gt; action = (s1, s2) =&gt; LongTimeProcess(s1, s2);
      CallAndWait&lt;int, string&gt;(action(1500, "hello"), 500);
  </code></pre>
  
  <p><strong>Updated code</strong>
  I've posted the code for future reference by forum users. The code below appears to work.
  The only point to check is that my unit test causes an exception to be thrown when calling the routine a second time on the same function at the point where we "action.EndInvoke(result)" as the result is not associated with the action. This is probably because my LongProcess is just a Thread.sleep, which in this instance will mean that it hasn't aborted by the time that my second call is made.</p>
  
  <pre><code>    public static void CallAndWait&lt;T1, T2&gt;(Action&lt;T1, T2&gt; action, T1 arg1, T2 arg2, int timeout)
  
      {
          Thread subThread = null;
          Action&lt;T1, T2&gt; wrappedAction = (p1, p2) =&gt;
          {
              subThread = Thread.CurrentThread;
              action(arg1, arg2);
          };
  
          IAsyncResult result = wrappedAction.BeginInvoke(arg1, arg2, null, null);
          if (((timeout != -1) &amp;&amp; !result.IsCompleted) &amp;&amp;
          (!result.AsyncWaitHandle.WaitOne(timeout, false) || !result.IsCompleted))
          {
              if (subThread != null)
              {
                  subThread.Abort();
              }
  
              //TODO: close external resource.
  
              throw new TimeoutException();
          }
          else
          {
              action.EndInvoke(result);
          }
      }
  </code></pre>