Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many models of Asynchronous development in .NET?

I am learning asynchronous programming using C# and I usually use BeginInvoke, but I am not very sure about the other methods of creating asynchronous application.

I have asked a question about this,see below link for more details:

How to return T value from BeginInvoke?

In above link, Gravell said that there are four models of asynchronous development

There's at least 4, then - a regular callback (non-APM, non-EAP) is also not uncommon

But Overflow said that there are three:

There are 3 models of asynchronous development in .NET

  • APM - (BeginXXX / EndXXX) which you are using here, when the long running task completes, it calls back into your code in the EndXXX method

  • EAP - Event based. In this model, when the long running task completes, an event is raised to inform your code.

  • TPL - New in .NET 4, this is the Task-based version. It looks most like synchronous programming to client code, using a fluent interface. Its calls back to your code using ContinueWith.

Anyone can help me on this?

I have searched google.com a lot, but actually they are using BeginInvoke most. thanks for your help.

like image 227
CharlieShi Avatar asked Feb 14 '12 03:02

CharlieShi


People also ask

What are asynchronous methods in C#?

C# has a language-level asynchronous programming model, which allows for easily writing asynchronous code without having to juggle callbacks or conform to a library that supports asynchrony. It follows what is known as the Task-based Asynchronous Pattern (TAP).

What is an asynchronous model?

An asynchronous model allows multiple things to happen at the same time. When you start an action, your program continues to run. When the action finishes, the program is informed and gets access to the result (for example, the data read from disk).

What is asynchronous method in VB net?

Return Types An async method is either a Sub procedure, or a Function procedure that has a return type of Task or Task<TResult>. The method cannot declare any ByRef parameters. You specify Task(Of TResult) for the return type of an async method if the Return statement of the method has an operand of type TResult.

What is asynchronous programming in .NET core?

It is a programming technique that allows us to execute our flows without blocking our application or causing the thread pool starvation. The often misconception is that by using the async and await keywords we gain better performance in terms of the speed of our application. But that's not the case.


2 Answers

Thread.Start - brutal

delegate.BeginInvoke/EndInvoke - 'old' standard

ThreadPool.QueueUserWorkItem - smart

TaskFactory.StartNew - the only way to do it correct (according to Patterns of parallel programming book | i recommend you to read it first for disambiguation)

like image 110
Mikant Avatar answered Oct 21 '22 02:10

Mikant


There's a lot that can be caught in the term "asynchronous development."


For one, you could want to execute code on a background thread. I recently updated a blog post of mine contrasting several common approaches to executing code in the background. Here's the list, in order from most desirable to least:

  1. Task (as used by async/await).
  2. Task (as used by the Task Parallel Library).
  3. BackgroundWorker.
  4. Delegate.BeginInvoke.
  5. ThreadPool.QueueUserWorkItem.
  6. Thread

On another hand, you could want to represent an asynchronous operation (which may or may not be actual code executing on a background thread). In that case, there are several approaches, in order from most desirable to least:

  1. Task (in the style of the Task-based Asynchronous Pattern (TAP))
  2. IAsyncResult with Begin*/End* methods (which has the unfortunate name Asynchronous Programming Model (APM)).
  3. A component written using the Event-based Asynchronous Pattern (EAP).

(As a side note, BackgroundWorker is EAP, and Delegate.BeginInvoke is APM).


On another hand, you could mean asynchronous programming in general, which can be interpreted to mean a reactive approach. In this case, there are only two approaches that I know of:

  1. Reactive Extensions (Rx).
  2. Event-based Asynchronous Pattern (EAP).

However, you could make a case that any event-driven program is reactive to some extent, so just handling UI events is a (simple) form of "asynchronous programming."


Also, these are only the common models. Any platform or library can add more. Here's some off the top of my head:

  • The Socket class has a special form of APM that can be used to minimize memory allocations. It works very similarly to APM but does not fit the pattern.
  • The WinRT runtime (coming in Windows 8) has its own representations of asynchronous operations (IAsyncOperation<TResult> and IAsyncInfo).
  • Windows Phone has specific support for a background agent, which permits you to run code in the background even if your app isn't currently running.
like image 24
Stephen Cleary Avatar answered Oct 21 '22 03:10

Stephen Cleary