12

C#: Make your delegates asynchronous from synchronous delegates

 4 years ago
source link: https://anthonygiretti.com/2021/05/01/c-make-your-delegates-asynchronous-from-synchronous-delegates/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

C#: Make your delegates asynchronous from synchronous delegates

2021-05-01 by anthonygiretti

Introduction

I wanted to write this post because I realized that a lot of developers have difficulty writing asynchronous delegates. I often see synchronous delegates in code review that could be transformed. So in this post I will show you how to proceede without however going over the usefulness of asynchronism, this is not the purpose of this post.

Synchronous delegates

There are 4 types of delegates in C #, you have delegates who:

  • Return a result
  • Do not return a result

but also delegates who:

  • Take one or more parameters
  • Do not take any parameters.

So that’s 4 ways to write a delegate, now let’s see what it looks like:

  1. Delegate that doesn’t take any parameter and doesn’t return any value: Action
  2. Delegate that takes one or several parameter and doesn’t return any value: Action<Tin>
  3. Delegate that doesn’t take any parameter and returns a value: Func<Tout>
  4. Delegate that doesn’t take any parameter and returns a value: Func<Tin, Tout>

Let’s look at a concrete example how this is implemented in practice:

using System;

namespace ConsoleApp1 { public class DoWork { // Action public void DoWorkA(Action action) { action(); }

// Action<Tin> public void DoWorkB(Action<string> action) { action("DoWorkB executed"); }

// Func<Tout> public bool DoWorkC(Func<bool> func) { return func(); }

// Func<Tin, Tout> public bool DoWorkD(Func<string, bool> func) { return func("DoWorkD executed"); } } class Program { static void Main(string[] args) { // Synchronous var doWork = new DoWork();

doWork.DoWorkA(() => Console.WriteLine("DoWorkA executed"));

doWork.DoWorkB((string param) => Console.WriteLine(param));

doWork.DoWorkC(() => { Console.WriteLine("DoWorkC executed"); return true; });

doWork.DoWorkD((string param) => { Console.WriteLine(param); return true; }); } } }

Synchronous to asynchronous delegates

The rule for obtaining a synchronous delegate is simple:
It doesn’t matter if this is an Action or a Func, the asynchronous version will always be a Func returning a Task which gives :

  1. Asynchronous Delegate that doesn’t take any parameter and doesn’t return any value: Func<Task>
  2. Asynchronous Delegate that takes one or several parameter and doesn’t return any value: Func<Tin, Task>
  3. Asynchronous Delegate that doesn’t take any parameter and returns a value: Func<Task<Tout>>
  4. Asynchronous Delegate that doesn’t take any parameter and returns a value: Func<Tin, Task<Tout>>

Example:

using System; using System.Threading.Tasks;

namespace ConsoleApp1 { public class DoWorkAsync { // Func<Task> public async Task DoWorkAAsync(Func<Task> task) { await task(); } // Func<Tin, Task> public async Task DoWorkBAsync(Func<string, Task> task) { await task("DoWorkBAsync executed"); }

// Func<Task<Tout>> public async Task<bool> DoWorkCAsync(Func<Task<bool>> task) { return await task(); }

// Func<Tin, Task<Tout>> public async Task<bool> DoWorkDAsync(Func<string, Task<bool>> task) { return await task("DoWorkDAsync executed"); } } class Program { static async Task Main(string[] args) { // Asynchronous Console.WriteLine();

var doWorkAsync = new DoWorkAsync();

await doWorkAsync.DoWorkAAsync(async () => await Task.Run(() => Console.WriteLine("DoWorkAAsync executed")) );

await doWorkAsync.DoWorkBAsync(async (string param) => await Task.Run(() => Console.WriteLine(param)) );

await doWorkAsync.DoWorkCAsync(async () => { await Task.Run(() => Console.WriteLine("DoWorkCAsync executed")); return true; });

await doWorkAsync.DoWorkDAsync(async (string param) => { await Task.Run(() => Console.WriteLine(param)); return true; }); } } }

That’s it!

I hope this tutorial helped you to understand how to transform your delegates into asynchronous delegates! Happy programming ?

Like this:

Loading...

Related posts


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK