Section 8.4. Asynchronous Programming


8.4. Asynchronous Programming

The asynchronous programming techniques that we have seen in Chapter 6 are applicable to Render Service as well. Depending on which application you are developing (for either Windows or the Web), you have two different options to use asynchronous programming for getting maps and enabling map interaction such as panning and zooming .

8.4.1. Asynchronous Programming for Windows Applications

When you use Visual Studio .NET to generate the MapPoint Web Service proxy class, it also generates the necessary methods for asynchronous programming. For example, if you look for the RenderServiceSoap.GetMap method, you also find the RenderServiceSoap.BeginGetMap and RenderServiceSoap.EndGetMap methods in the proxy class. The Begin and End method pairs together enable the asynchronous programming patterns for your Web Service client applications. Using these methods is easy; in a synchronous scenario, your Find call looks as follows:

     //Call the GetMap Method     MapImage[] mapImages = renderSoap.GetMap(mapSpec);     //Now display the map     . . .

If this code is running on the UI thread, it gets blocked until the GetMap returns the map image array and results in an unresponsive application during that time. To avoid this situation, create a worker thread and use it to call GetMap methods so that your UI thread is free during this long network round-trip. In fact, that's exactly what the BeginGetMap and EndGetMap methods do behind the scenes. To implement the previous code using asynchronous methods, you would do something similar to the following code. First, define a "callback" method for your asynchronous method calls:

     private void RenderServiceCallback(IAsyncResult ar)     {        RenderServiceSoap renderSoap           = ar.AsyncState as RenderServiceSoap;        if(renderSoap == null)          return;        MapImage[] mapImages = renderSoap.EndGetMap(ar);        //Display map        . . .     }

Next, modify your GetMap call to be an asynchronous BeginGetMap call:

     //Async call to GetMap     AsyncCallback callback = new AsyncCallback(RenderServiceCallback);     rendersoap.BeginGetMap(mapspec, callback, rendersoap);

The BeginGetMap call invokes the GetMap method on a different (worker) thread and passes a pointer to the RenderSeviceCallback method as a "callback" method; when the GetMap method returns the MapResult array, the callback delegate is invoked so that the RenderServiceCallback method gets executed on the UI thread again. In the RenderServiceCallback method, you need to obtain the MapImage array returned by the GetMap method by calling the EndGetMap method and displaying it. Keep in mind that the HTTP session is kept alive during this asynchronous operation behind the scenesso this pattern is only asynchronous at your application thread level, not at the HTTP communication level.

8.4.2. Asynchronous Programming for Web Applications

As with AJAX, discussed in Chapter 6, you can use the combination of XML over HTTP and asynchronous JavaScript to create web applications that give the user a rich experience.

8.4.2.1. AJAX-Enabling Your Web Applications

Since the basics of AJAX implementation have already been covered in Chapter 6, in this section I will discuss the items specific to render. To AJAX-enable your rendering, you need to implement:

  • An ASP.NET page (or an HTTP handler ) that can take a place or an address and return map URL from MapPoint Web Service

  • AJAX JavaScript and an image (img tag) on the web client that invokes the previously mentioned ASP.NET page and retrieves the map URL to display in the page

Also, when you are implementing the ASP.NET page that returns the map URL to the HTML page, you have to cache the corresponding MapSpecification at HTTP Session scope to enable map interaction, such as panning and zooming.




Programming MapPoint in  .NET
Programming MapPoint in .NET
ISBN: 0596009062
EAN: 2147483647
Year: 2005
Pages: 136
Authors: Chandu Thota

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net