Cinchoo – Delegate.WaitFor(), Extension Method

Here I’ll talk about WaitFor extension method. This extension method used to run a method with finite number of retries if it failed to run at first time and/or in a finite time frame. It provides most of Action and Func delgates overloads.

1. Add reference to Cinchoo.Core.dll assembly

2. Namespace Cinchoo.Core

Sample 1:

Below are the list of overloads for Func<T> delegate, Cinchoo provides these overloads almost all the Func’s and Action’s.

public static TResult WaitFor(this Func func);
public static TResult WaitFor(this Func func, int timeout);
public static TResult WaitFor(this Func func, int timeout, int maxNoOfRetry);
public static TResult WaitFor(this Func func, int timeout, int maxNoOfRetry, int sleepBetweenRetry);

Below sample shows you how to run a  Func<T> delegate method using WaitFor() extension method.

class Program
{
    static void Main(string[] args)
    {
		Func f1 = () =&gt;
			{
				System.Threading.Thread.Sleep(1);
                Console.WriteLine(&quot;Running method...&quot;);
				return 1;
			};

        Console.WriteLine(&quot;Output: {0}&quot;.FormatString(f1.WaitFor()));
	}
}

When you run the above code, the output will be

Running method...
Output: 1
Press any key to continue . . .

Sample 2:

In this example, I’ll show you the way of invoking a method with timeout (1000ms) parameter. It throws a timeout exception, as the method takes more time to execute than timeout (1000 ms) period.

class Program
{
    static void Main(string[] args)
    {
        try
        {
            Func f1 = () =&gt;
                {
                    System.Threading.Thread.Sleep(5000);
                    Console.WriteLine(&quot;Running method...&quot;);
                    return 1;
                };

            Console.WriteLine(&quot;Output: {0}&quot;.FormatString(f1.WaitFor(1000)));
        }
        catch (Exception ex)
        {
            Console.WriteLine(&quot;Error: {0}&quot;.FormatString(ex.Message));
        }
	}
}

When you run the above code, the output will be

Error: Timeout [1000 ms] elapsed prior to completion of the method [Target: , Metho
d: Int32 b__0()].
Press any key to continue . . .

Sample 3:

In here, we will see how a method with an error is executed for number of times with one of the WaitFor overload.

static void Main(string[] args)
{
    try
    {
        Func&lt;int&gt; f1 = () =&gt;
            {
                System.Threading.Thread.Sleep(10);
                throw new ApplicationException(&quot;Communication error.&quot;);
            };

        Console.WriteLine(&quot;Output: {0}&quot;.FormatString(f1.WaitFor(Timeout.Infinite, 3)));
    }
    catch (Exception ex)
    {
        Console.WriteLine(&quot;Error: {0}&quot;.FormatString(ex.Message));
    }
}

When you run the above code, the output will be

Error: Exception(s) occurred : The method failed to execute after 3 retries..
[ System.ApplicationException: Communication error.
   at Cinchoo.Core.ChoWaitForTest.Program.&lt;Main&gt;b__0() in C:\Personal\Cinchoo.Fr
amwork\Cinchoo.Core.Test\Cinchoo.Core.ChoWaitFor.Test\Program.cs:line 18
   at Cinchoo.Core.ChoWaitFor.WaitFor[TResult](Func`1 func, Int32 timeout, Int32
 maxNoOfRetry, Int32 sleepBetweenRetry) in C:\Personal\Cinchoo.Framwork\Cinchoo.
Framework\Cinchoo.Core\WaitFor\ChoWaitFor.cs:line 148 ]

[ System.ApplicationException: Communication error.
   at Cinchoo.Core.ChoWaitForTest.Program.&lt;Main&gt;b__0() in C:\Personal\Cinchoo.Fr
amwork\Cinchoo.Core.Test\Cinchoo.Core.ChoWaitFor.Test\Program.cs:line 18
   at Cinchoo.Core.ChoWaitFor.WaitFor[TResult](Func`1 func, Int32 timeout, Int32
 maxNoOfRetry, Int32 sleepBetweenRetry) in C:\Personal\Cinchoo.Framwork\Cinchoo.
Framework\Cinchoo.Core\WaitFor\ChoWaitFor.cs:line 148 ]

[ System.ApplicationException: Communication error.
   at Cinchoo.Core.ChoWaitForTest.Program.&lt;Main&gt;b__0() in C:\Personal\Cinchoo.Fr
amwork\Cinchoo.Core.Test\Cinchoo.Core.ChoWaitFor.Test\Program.cs:line 18
   at Cinchoo.Core.ChoWaitFor.WaitFor[TResult](Func`1 func, Int32 timeout, Int32
 maxNoOfRetry, Int32 sleepBetweenRetry) in C:\Personal\Cinchoo.Framwork\Cinchoo.
Framework\Cinchoo.Core\WaitFor\ChoWaitFor.cs:line 148 ]
Press any key to continue . . .

Happy coding!!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s