Asynchronous operation and flow in C #

Asynchronous programming is a method that calls a long method in the background so that the user interface thread remains responsive. It should be used when invoking a web service or database request or any associated I / O operation. when the asynchronous method completes, it returns the result to the main thread. Thus, the main thread of the program should not wait for the result of the I / O binding operation and continues to run without blocking / freezing the user interface. This is normal.

As far as I know, the asynchronous method runs on the background thread. Runtime makes a thread available either from a thread, or it can create a completely new thread to execute it.

But in many posts, I read that an asynchronous operation can be performed in a separate thread or without using any thread. Now I am very confused.

1) Could you help clarify in what situation the asynchronous operation will not use the stream?

2) What is the role of the processor core in asynchronous operation?

3) How is it different from multithreading? I know one thing that multithreading is used with processing through computation.

Please, help.

+6
source share
3 answers

IO (let's say database operation over a network) is a good example for all three:

  • you basically just register the callback that the OS will finally call (possibly in a newly created thread) when the I / O operation completes. There is no thread sitting and waiting - the resurrection will be caused by hardware events (or, at least, with the help of the OS, usually outside the user space)

  • it may not have (see 1)

  • In multithreading, you use multiple threads (your background thread), and there you could be inactive without doing anything (but using system resources) - this, of course, is different if you have something to calculate (so the thread does not stop waiting external results) - it makes sense to use a background worker thread

+5
source

Asynchronous operations do not really mean much about how they are processed, but only that they would like to be able to get back to you later with your results. As an example:

  • They can (as you already mentioned) separate the task related to computing into an independent thread, but this is not the only precedent.
  • Sometimes they can be executed synchronously within the caller that starts them, in which case the additional thread is not used. This can happen with an I / O request if there is already enough buffer (input) or buffer (output) free space to serve the request.
  • They may simply refuse a long-term I / O request to the system; in this case, the callback is likely to occur in the background thread after receiving notification from the I / O completion port.
  • Upon completion, the callback can be delivered later on the same thread; this is especially true for UI events, such as navigation in WebBrowser .
+1
source

Asynchronity doesn't say anything about a thread. Its about the presence of some kind of callbacks that will be processed inside the "statemachine" (not quite right, but you can think of it as events). Asynchrony does not create threads or allocate system resources. You can run as many asynchronous methods as you want.

Streams really make sense on your system, and you have a hughe, but a limited number that you can have right away.

Io operations are mainly related to other controllers (HDD, NIC, ...) What now happens if you create a thread is that your application thread, which has nothing to do, is waiting for the controllers to finish working. In async, like Karsten and Jeffrey already mentioned that you just get some kind of callback mechanism, so your thread continues to do other work, methods, etc.

Also keep in mind that each thread is worth the resources (RAM, Performance, Handles Collection Garbage Collection is degrading, ...) and may even be in exceptions (OutOfMemoryException ...)

So, when to use Threads? Absolutely only if you really need it. If there is an async api, use it if you have no really important reason not to use it. In the past days, asynchronous api was very painful, so many people used threads when they needed just asynchrony.

For example, node.js generally refuses to use mulptile thread!

This is especially important if you are processing multiple requests, for example, in services / websites where there is always work. There is also a short broadcast with Jeffrey Richter about this that helped me understand

See also the MSDN PS article: How side-effect source code with asynchronous and wait is generally more readable.

0
source

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


All Articles