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.
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).
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).
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.
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.
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)
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:
Task
(as used by async/await).Task
(as used by the Task Parallel Library).BackgroundWorker
.Delegate.BeginInvoke
.ThreadPool.QueueUserWorkItem
.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:
Task
(in the style of the Task-based Asynchronous Pattern (TAP))IAsyncResult
with Begin*
/End*
methods (which has the unfortunate name Asynchronous Programming Model (APM)).(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:
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:
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.IAsyncOperation<TResult>
and IAsyncInfo
).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With