Asynchronous Programming Lesson The NET Asynchronous Programming Model

  • Slides: 11
Download presentation
Asynchronous Programming

Asynchronous Programming

Lesson: The. NET Asynchronous Programming Model n What Is Asynchronous Programming? n Asynchronous Programming

Lesson: The. NET Asynchronous Programming Model n What Is Asynchronous Programming? n Asynchronous Programming Support in the. NET Framework

What Is Asynchronous Programming? n An application gives some work to other thread(s) while

What Is Asynchronous Programming? n An application gives some work to other thread(s) while it continues doing other work on the main thread Worker thread Main thread S u b m i t r e p o r ts to s e r v e r n Useful in Windows Forms applications so users will not be blocked waiting for results and can instead continue with other work Submit reports Create new reports

Asynchronous Programming Support in the. NET Framework n n A design pattern for asynchronous

Asynchronous Programming Support in the. NET Framework n n A design pattern for asynchronous programming l Used by the. NET Framework to make asynchronous calls uniform across different parts of the framework l User-created classes that support asynchronous calls should conform to this design pattern Asynchronous support is provided in many of the logical areas l I/O, sockets, networking, ASP. NET and XML Web services, messaging, and asynchronous delegates l Implementation is transparent, call the appropriate methods and let the NET Framework handle the details

Lesson: The Asynchronous Programming Model Design Pattern NO Asynchronous? YES Choose completion mechanism //

Lesson: The Asynchronous Programming Model Design Pattern NO Asynchronous? YES Choose completion mechanism // // // . . . call Operation. . . wait for return. . . continue processing. . . // // // . . . call Begin. Operation. . . operation begun on another thread. . . continue with other processing. . . receive results. . . process results. . .

Overview of the Asynchronous Programming Model Design Pattern n Caller decides whether a particular

Overview of the Asynchronous Programming Model Design Pattern n Caller decides whether a particular call should be asynchronous n Asynchronous operation logically split into two parts 1. Client begins the operation by calling the Begin. Operation method 2. Client notified that operation is complete and receives results Completion Technique Comments Use a callback Supply a callback delegate, method will be called when operation completes Poll the IAsync. Result interface’s Is. Completed property Call the End. Operation method and block till operation completes Wait on a handle Wait on IAsync. Result interface’s Wait. Handle property, then call End. Operation method

Using the Design Pattern with an Asynchronous Callback for Completion Create the asynchronous callback

Using the Design Pattern with an Asynchronous Callback for Completion Create the asynchronous callback delegate Invoke the Begin. Operation method, passing it the callback delegate Inside the callback, invoke the End. Operation method to notify that asynchronous work is complete and to return results Return control to the main thread and update UI Main thread Begin. Operation {…} Update UI Thread from thread pool Get reports Callback Delegate{ End. Operation }

How to Set Up and Initiate the Call Create the asynchronous callback delegate Async.

How to Set Up and Initiate the Call Create the asynchronous callback delegate Async. Callback del. CB = new Async. Callback( this. Async. CB); Invoke the Begin. Operation method, passing it the callback delegate Invoke the Begin. Operation method WS. Begin. Get. Reports. For. Employee( username, pwd. Token, Record. Cursor, 10, Total. Num. Records, del. CB, null); Callback delegate is passed in to the Begin. Operation method

How to Receive Completion Notification and Results Inside the callback, invoke the End. Operation

How to Receive Completion Notification and Results Inside the callback, invoke the End. Operation method to retrieve the results of the asynchronous call // Inside the callback method, Async. CB, call // End. Operation to get results of the async call void Async. CB (IAsync. Result ar) { Invoke End. Operation method. . . Data. Set ds = WS. End. Get. Reports. For. Employee( ar, out Total. Num. Records); . . . } Receive results

How to Return Control to the Main Thread In Windows Forms applications, any calls

How to Return Control to the Main Thread In Windows Forms applications, any calls to methods or properties for controls on the form must be done on the main thread Return control to the main thread //Switch back to main thread to update the UI //First, create a Method. Invoker delegate for //the method to be called Method. Invoker mi = new Method. Invoker( this. Update. UI); // Use the current form’s Begin. Invoke to // invoke the delegate This. Invoke(mi);

Animation: Making an Asynchronous Call to an XML Web Service In this animation, you

Animation: Making an Asynchronous Call to an XML Web Service In this animation, you will see how an object makes an asynchronous call to an XML Web service