Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 330 -.NET Applications1 Chapter 7 – Asynchronous Calls.

Similar presentations


Presentation on theme: "CIS 330 -.NET Applications1 Chapter 7 – Asynchronous Calls."— Presentation transcript:

1 CIS 330 -.NET Applications1 Chapter 7 – Asynchronous Calls

2 CIS 330 -.NET Applications2 Objectives Asynchronous System Requirements.NET Asynchronous Implementation

3 CIS 330 -.NET Applications3 Modern Asynchronous Requirements Components should support both synchronous and asynchronous calls Clients should be able to select either call mode Clients should be able to issue multiple asynchronous calls Components should be able to serve multiple concurrent calls All component method output parameters or return values must be accessible by clients Errors on the component side should have a way to be propagated back to the client Asynchronous calls should be straightforward and simple to use, e.g., implementation details should be hidden

4 CIS 330 -.NET Applications4 Client Choices Perform work while call is in progress, then block until completion Perform work while call is in progress, then poll for completion Perform work while call is in progress, wait for a predetermined amount of time, then stop waiting, even if the method execution has not yet completed Wait simultaneously for completion of multiple methods. Client can choose to wait for all or any of the pending calls to complete Receive notifications when the method has completed. The notification will be in the form of a callback on the client- provided method

5 CIS 330 -.NET Applications5 Asynchronous Call Programming.NET utilizes a thread pool to manage all asynchronous calls.NET used two predefined methods –BeginInvoke(): initiates an asynchronous method call –EndInvoke(): manages method completion – namely retrieving output parameters, return values, and error handling.NET uses delegates All.NET classes support asynchronous invocation

6 CIS 330 -.NET Applications6 Simple Asynchronous execution Sequence Calculator calculator = new Calculator(); BinaryOperations oppDel = calculator.Add; IAsyncResult asyncResult1 = oppDel.BeginInvoke(2,3,null,null); IAsyncResult asyncResult2 = oppDel.BeginInvoke(4,5,null,null); // do some work int result; result = oppDel.EndInvoke(asyncResult1); //may block Console.WriteLine (“Result = {0}“,result); result = oppDel.EndInvoke(asyncResult2); Console.WriteLine ((“Result = {0}“,result);

7 CIS 330 -.NET Applications7 Using IAsyncResult.AsyncWaitHandle Calculator calculator = new Calculator(); BinaryOperations oppDel = calculator.Add; IAsyncResult asyncResult = oppDel.BeginInvoke(2,3,null,null); while (asyncResult.IsCompleted == false) //polling { asyncResult.AsyncWaitHandle.WaitOne(10,false); // do some work } int result; result = oppDel.EndInvoke(asyncResult1); //will not block Console.WriteLine (“Result = {0}“,result);

8 CIS 330 -.NET Applications8 Using Completion Callback Methods Public class CalculatorClient { public void AsyncAdd() { Calculator calculator = new Calculator(); BinaryOperations oppDel = calculator.Add; oppDel.BeginInvoke(2,3,OnMethodCompletion,null); } void OnMethodCompletion(IAsyncResult asyncResult) { int result = 0; AsyncResult resultObj = (AsyncResult)asyncResult; Debug.assert(resultObj.EndInvokeCalled = false); BinaryOperation oppDel = (BinaryOperation)resultObj.AsyncDelegate; result = oppDel.EndInvoke(asyncResult1); //will not block Console.WriteLine (“Result = {0}“,result); }

9 CIS 330 -.NET Applications9 Completion Callback Model Preferred model for event-driven applications Event-driven applications –Has methods that trigger events and methods that handle these events –Easier to manage multiple threads, events, and messages –Greater scalability, responsiveness, and performance Waiting, blocking, and polling are for applications that are strict, predictable, and deterministic Callback methods must be reentrant and thread-safe

10 CIS 330 -.NET Applications10 Summary Asynchronous System Requirements.NET Asynchronous Implementation


Download ppt "CIS 330 -.NET Applications1 Chapter 7 – Asynchronous Calls."

Similar presentations


Ads by Google