Leveraging Func<Task> and Getting Funky

Asynchronous programming is a bit nightmarish, but hopefully this sheds some light upon how you can leverage certain delegate types. Take this example, where you want an abstraction layer for calling a block of code up to a set number of attempts until it succeeds, otherwise having it delegate back the exception you’ve obtained on the very last attempt:

class Program
{
	static void Main(string[] args)
	{
		// Since this is a console application, I just ran everything inside of an async task.
		Task.Run(async () => {
			bool throwException = true;
			var firstResult = await AttemptBlock.Run(async () => {
				Console.WriteLine("X called.");
				if (throwException)
				{
					throwException = false;
					throw new Exception();
				}
				await AttemptBlock.Run(async () =>
				{
					Console.WriteLine("Y called.");
					await Z();
					Console.WriteLine("Y finished.");
				}, 3, async (exception) =>
				{
					await Task.Run(() =>
					{
						Console.WriteLine("Y exception handler called.");
					});
				});
				Console.WriteLine("X finished.");
			}, 3, async (exception) =>
			{
				await Task.Run(() =>
				{
					Console.WriteLine("X exception handler called.");
				});
			});
			Console.WriteLine("Finished the first block with {0}.", firstResult);
			var secondResult = await AttemptBlock.Run(async () =>
			{
				Console.WriteLine("O called.");
				await Z();
				throw new Exception();
			}, 3, async (exception) =>
			{
				await Task.Run(() =>
				{
					Console.WriteLine("O exception handler called.");
					throw new Exception();
				});
			});
			Console.WriteLine("Finished the second block with {0}.", secondResult);
		});
		Console.ReadLine();
	}

	private async static Task<bool> Z()
	{
		Console.WriteLine("Z called.");
		await Task.Delay(1000);
		Console.WriteLine("Z finished.");
		return true;
	}
}

class AttemptBlock
{
	public async static Task<bool> Run(Func<Task> function, int attempts, Func<Exception, Task> handler = null)
	{
		while (attempts > 0)
		{
			var candidate = (Exception)null;
			try {
				await function();
				return true;
			} // If this try-catch block was not here, the function could deadlock if it threw an exception.
			catch (Exception exception) {
				candidate = exception;
			}
			attempts--;
			if (attempts <= 0 && handler != null && candidate != null)
				try {
					await handler(candidate);
				}
				catch { } // If this try-catch block was not here, the handler could deadlock if it threw an exception.
		}
		return false;
	}
}

The output is as follows:

X called.
X called.
Y called.
Z called.
Z finished.
Y finished.
X finished.
Finished the first block with True.
O called.
Z called.
Z finished.
O called.
Z called.
Z finished.
O called.
Z called.
Z finished.
O exception handler called.
Finished the second block with False.

You may also want to check out this post on Synchronous and Asynchronous Delegate Types.

Alexandru

"To avoid criticism, say nothing, do nothing, be nothing." - Aristotle

"It is wise to direct your anger towards problems - not people; to focus your energies on answers - not excuses." - William Arthur Ward

"Science does not know its debt to imagination." - Ralph Waldo Emerson

"Money was never a big motivation for me, except as a way to keep score. The real excitement is playing the game." - Donald Trump

"All our dreams can come true, if we have the courage to pursue them." - Walt Disney

"Mitch flashes back to a basketball game held in the Brandeis University gymnasium in 1979. The team is doing well and chants, 'We're number one!' Morrie stands and shouts, 'What's wrong with being number two?' The students fall silent." - Tuesdays with Morrie

I'm not entirely sure what makes me successful in general programming or development, but to any newcomers to this blood-sport, my best guess would be that success in programming comes from some strange combination of interest, persistence, patience, instincts (for example, someone might tell you that something can't be done, or that it can't be done a certain way, but you just know that can't be true, or you look at a piece of code and know something doesn't seem right with it at first glance, but you can't quite put your finger on it until you think it through some more), fearlessness of tinkering, and an ability to take advice because you should be humble. Its okay to be wrong or to have a bad approach, realize it, and try to find a better one, and even better to be wrong and find a better approach to solve something than to have had a bad approach to begin with. I hope that whatever fragments of information I sprinkle across here help those who hit the same roadblocks.

Leave a Reply

Your email address will not be published. Required fields are marked *