Need help in refactoring Retry logic pattern
<p>I want to send some commands to the serial port and listen for a response.
The response from the port could be well formed data or corrupt data. In that case I have to retry sending the same command after a strategic timeout</p>

<p>At the moment I'm using a while loop to achieve this. However I'm looking for a more simple and reusable approach for all the ~30 commands that I need to send.</p>

<pre><code>int retryCount = 3;

while(retryCount &gt; 0){
 _SerialPort.WriteLine("$0\r");
 Thread.Sleep(300);

 //Check response 
 RESPONSE_STATE iResponse = ProcessResponse();
 if (iResponse == RESPONSE_STATE.NOERROR)
  {
     //process response
     break;
  }
  else
  {
     retryCount--;
     //repeat operation
  }
}
</code></pre>

<p>I want to implement a Generic method something similar to below, Which I can call once and it shall take care of retrying logic implementation. Instead of the exception handling I want to query for the response from the serial port. </p>

<pre><code>public T Do&lt;T&gt;(Func&lt;T&gt; action, TimeSpan retryInterval, int retryCount = 3)
            {
                var exceptions = new List&lt;Exception&gt;();
                for (int retry = 0; retry &lt; retryCount; retry++)
                {
                    try
                    {
                        if (retry &gt; 0)
                            Thread.Sleep(retryInterval);
                        return action();
                    }
                    catch (Exception ex)
                    {
                       exceptions.Add(ex);
                    }
                }
                throw new AggregateException(exceptions);
            }
</code></pre>

<p>Ex :  <code>int returnCount = Do(MyMethod,TimeSpan.FromMilliSeconds(300),3);</code></p>

<p>How do I achieve this?</p>