Asynchronous programming in .Net The advancement
in software technology is so rapid that it has necessitated the usage
of high-end processors for executing the complex logic involved in it.
It has also now become very common to execute code on multiple processors.
Asynchronous
programming is about executing part of code on separate threads. A thread
is a sequence of execution in program. The .Net framework has implemented
the feature of asynchronous programming through the Asynchronous Programming
(APM) Model by allowing to executing tasks in a non-linear way. It provides
rich set of classes implementing the asynchronous operations and prescribes
a standardized mechanism that can allow executing them without directly
working with the threads. With this, the application developed can perform
faster and better, be more responsive and utilize system resources optimally. The APM involves executing an asynchronous operation by calling a method (starting with Begin) with the relevant data. To control the execution of this operation at its end stage, the terminating method (starting with End) is called in different ways. For example, the method, BeginRead () and EndRead () of the FileStream class is used to read the bytes of a file asynchronously. While initiating the asynchronous call, an object of type, IASyncResult is returned with values filled with relevant information. The same IAysncResult object is also passed as parameter to the method called during the termination of the asynchronous method. Based on the way in which the end of the asynchronous call is handled in the code, the .Net framework has classified three styles of APM as listed below: Wait-until-Done
model: Polling
model: Callback
model: Some of the helper classes designed to aid APM are ThreadPool and Timer included in the System.Threading namespace. ThreadPool is used for getting a thread (already instantiated and maintained by the framework) from a pool of threads for performing asynchronous programming. The main aim of using ThreadPool is that it saves time for the application since it reuses existing threads without any setup tasks executed for the thread. Also, the code size and hence the developing effort is greatly reduced since the thread management tasks like creating, scheduling and termination are taken care of by the framework itself. Timer is another important helper class used for creating periodically reoccurring routines. Object of this class will fire an asynchronous call to a method based on time. Options to specify the callback routine, time interval, etc. can be specified during the creation of the Timer object. Another class called SynchronizationContext class is used usually to fire an asynchronous call. This class provides two methods for asynchronous execution which does not return any value or object. The Send method executes the asynchronous code in a separate thread and also blocks the execution of the caller until it returns. This is contrary to the method Post wherein the asynchronous code is executed without blocking the execution of caller. Tips:
FREE
Subscription
Subscribe
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________ |