Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous operation and thread in C#

Asynchronous programming is a technique that calls a long running method in the background so that the UI thread remains responsive. It should be used while calling a web service or database query or any I/O bound operation. when the asynchronous method completes, it returns the result to the main thread. In this way, the program's main thread does not have to wait for the result of an I/O bound operation and continues to execute further without blocking/freezing the UI. This is ok.

As far as I know the asynchronous method executes on a background worker thread. The runtime makes availabe the thread either from the threadpool or it may create a brand new thread for its execution.

But I have read in many posts that an asynchronous operation may execute on a separate thread or without using any thread. Now I am very confused.

1) Could you please help clarifying in what situation an asynchronous operation will not use a thread?

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

3) How it is different from multithreading? I know one thing that multithreading is usefult with compute-bound operation.

Please help.

like image 259
Tisha Anand Avatar asked Jun 03 '15 04:06

Tisha Anand


2 Answers

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

  1. you basically just register a callback the OS will finally call (maybe on a then newly created thread) when the IO-Operation finished. There is no thread sitting around and waiting - the resurrection will be triggered by hardware-events (or at least by a OS process usually outside user-space)

  2. it might have none (see 1)

  3. in Multithreading you use more than one thread (your background-thread) and there one might idle sit there doing nothing (but using up system-resources) - this is of course different if you have something to compute (so the thread is not idle waiting for external results) - there it makes sense to use a background-worker-thread

like image 135
Random Dev Avatar answered Oct 25 '22 22:10

Random Dev


Asynchronous operations don't actually imply much of anything about how they are processed, only that they would like the option to get back to you later with your results. By way of example:

  • They may (as you've mentioned) split off a compute-bound task onto an independent thread, but this is not the only use case.
  • They may sometimes complete synchronously within the call that launches them, in which case no additional thread is used. This may happen with an I/O request if there is already enough buffer content (input) or free buffer space (output) to service the request.
  • They may simply drop off a long-running I/O request to the system; in this case the callback is likely to occur on a background thread after receiving notification from an I/O completion port.
  • On completion, a callback may be delivered later on the same thread; this is especially common with events within a UI framework, such as navigation in a WebBrowser.
like image 34
Jeffrey Hantin Avatar answered Oct 25 '22 23:10

Jeffrey Hantin