Writing an Asynchronous Process to Expect

I am learning how to convert my (synchronous) algorithms to asynchronous. (CLICK) First, just to be clear, this question doesn’t concern “what is Asink and Waiting” (I have already read Steven Cleary’s excellent posts, such as Async and Await (If anyone is interested to read the link - it is very informative)

I also read the chapter on concurrency "C # in a nutshell."

This question is not related to how async functions are used to call functions. I already know that.

Unfortunately, in almost all of the things I read, await Task.Delay(10) used to "create an asynchronous function." For instance:

 public async Task<int> GetResult() { int result= await GiveMeTheInt(); } public async Task<int> GiveMeTheInt() //<--is async needed here? (oops! I just realize it is... { await Task.Delay(100); return(10); } 

In this example, for example, I already understand that the magic of async is waiting in the GetResult() function, but the implementation of GiveMeTheInt() not very useful. (They just put Delay as a generic asynchronous function and leave it with it)

So my question is about the type of "GiveMeTheInt" questions (and not those who call them).

Question

If I have an algorithm written in a function that has so far been synchronous, how can I convert it for asynchronous use?


This is not a duplicate question, the closest I found Including a synchronous async method , in which a poster is invited to use an asynchronous version of its method that already exists. In my case, this does not exist.

My algorithms consist mainly of image processing, so essentially scanning a large array and changing the values ​​of each pixel. Sort of

 void DoSomethingToImage(int[,] Image) { for(int i=0;i<width;i++) for(int j=0;j<height;j++) { Image[i,j]=255; } } 

(This is a fictional example, the operation, of course, is different)

The closest I got an answer to this is to put the function inside Task.Run() , but I'm not sure if this is the way to do it.

Any help would be greatly appreciated.

+5
source share
1 answer

So take a look at your method:

 void DoSomethingToImage(int[,] image) { for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { image[i, j] = 255; } } } 

Is it asynchronous? Obviously not. This is just processor related work that will delay the processor a bit. Thus, it is not very good to do asynchronous by itself. It should be run synchronously.

What if you consume this from the asynchronous part of the application? Of course, you do not want the user interface to be blocked because you are repeating a lot of pixels. So the solution is to load the work into another thread. You do this with Task.Run :

 await Task.Run(() => DoSomethingToImage(image)); 

So you should write this when you call the DoSomethingToImage method from an asynchronous method. Now, if you use this method only in asynchronous contexts, you can argue that it makes sense to move Task.Run to a function:

 Task DoSomethingToImageAsync(int[,] image) { return Task.Run(() => { … }); } 

Is that a good idea? Generally not, because now you are making the method asynchronous when it is not actually there. All he does is create a new thread that does this work and then waits for the thread to finish. So, now you hide this part, and also make a method that performs highly synchronous work, decide that the thread should be started. This is rarely a good idea. And there is nothing wrong with maintaining a method as it is, showing that it is synchronous, and making the calling code responsible for deciding how that code should be run.

If I have an algorithm written in a function that has so far been synchronous, how can I convert it for asynchronous use?

So, getting back to your actual question, this is actually hard to answer. The answer is probably: "It depends."

If the method works with the CPU, you better not synchronize it and let the calling code decide how to start it. If you are primarily involved in I / O, where you interact with other interfaces (network, file system, etc.), then this is a good candidate for its asynchrony, especially considering that many of these interfaces already offer asynchronous ways of interacting with them.


One final note about your "asynchronous need here?" the question is in your code: you need the async keyword when you want to use await inside it. The mere presence of the async does not make the method asynchronous, though (even the return type does not indicate this).

+7
source

Source: https://habr.com/ru/post/1272493/


All Articles