Search KB Filter article types
HtmlBox CodeSample - Asynchronous loading of contents
Categories: DHTML, How-to
Tags: Architects, Developers, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
Revision: 1
Posted: 12/Feb/2010
Updated: 23/July/2010
Status: Publish
Types: Code

This article will have a few sections added to it soon, based on the following article type skeleton: CodeSample

Overview

Most browsers (if not all of them) have only one thread, which means that while a script is loading in a form or one of it's frames, the others simply wait and not respond to user actions. For loading of contents to an HtmlBox that takes a considerable amount of time, this makes your application non-responsive until the load compleates.

This article presents a best-practice, of using an asynchronous pattern for loading the HtmlBox's contents via BeginInvoke initiated delegate with an asynchronous callback and then a Timer to make sure your UI gets updated.

What exactly goes on behind the scenes threadwise, can be read in the referenced articles in the References section. What this does to, or more precisely for, your application is clearly demonstrated by the attached demo application. The application loads a "long running" contents into two HtmlBoxes. The contents will delay the load for 5 seconds before returning. One of the buttons will load the contents into each HtmlBox one after the other, with a total loading time of 10 seconds, and while waiting, the application does not respond to UI requests like mouse clicks. The other button uses the BeginInvoke method which will load the contents in parallel, both at the same time, and the total loading time will be 5 seconds with the application fully responding to UI requests while loading.

The downside of using this method is that the asynchronous callback will reach your server-side code, and as with other updates coming in from a different thread than the UI thread, the server can not initiate the update of the browser UI. That update must be initiated from the browser. You can choose to just leave it that way, and your UI will be updated as a part of the next request your browser sends to the server, or you can do as we chose to do in this demo application, to add a Timer to your form which will make sure those requests will be made on a regular basis.

Samples of useVB.NET Code
' Delegate for loading HtmlBox with data
Public Delegate Sub LongTimeTask_Delegate(ByVal strUrl As String, ByVal objHtml As HtmlBox, ByVal strMessage As String)
 
' The actual data loading for the HtmlBox - The long running process
Public Sub LoadHtmlBox(ByVal strUrl As String, ByVal objHtml As HtmlBox, ByVal strmessage As String)
    objHtml.Html = strmessage
    GetDelayFormData(objHtml, strUrl)
End Sub
 
 
Private Sub btnLoadWithInvoke_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadWithInvoke.Click
    Dim d As LongTimeTask_Delegate
    Dim h1, h2 As IAsyncResult
    d = New LongTimeTask_Delegate(AddressOf LoadHtmlBox)
    h1 = d.BeginInvoke(BuildPath, HtmlBox1, "<HTML>Being Loaded by BeginInvoke</HTML>", New AsyncCallback(AddressOf TaskCompleted), HtmlBox1)
    d = New LongTimeTask_Delegate(AddressOf LoadHtmlBox)
    h2 = d.BeginInvoke(BuildPath, _
                       HtmlBox2, "<HTML>Being Loaded by BeginInvoke</HTML>", New AsyncCallback(AddressOf TaskCompleted), HtmlBox2)
End Sub

C# Code
//  Delegate for loading HtmlBox with data
public delegate void LongTimeTask_Delegate(string strUrl, HtmlBox objHtml, string strMessage);
 
public void LoadHtmlBox(string strUrl, HtmlBox objHtml, string strmessage)
{
    objHtml.Html = strmessage;
    GetDelayFormData(objHtml, strUrl);
}
 
private void btnLoadWithInvoke_Click(object sender, System.EventArgs e)
{
    LongTimeTask_Delegate d;
    IAsyncResult h1;
    IAsyncResult h2;
    AsyncCallback ACB;
 
    d = new LongTimeTask_Delegate(this.LoadHtmlBox);
    ACB = new AsyncCallback (this.TaskCompleted);
    h1 = d.BeginInvoke(this.BuildPath(), HtmlBox1, "<HTML>Being Loaded by BeginInvoke</HTML>", ACB, this.HtmlBox1);
    d = new LongTimeTask_Delegate(this.LoadHtmlBox);
    h2 = d.BeginInvoke(this.BuildPath(), HtmlBox2, "<HTML>Being Loaded by BeginInvoke</HTML>", ACB, this.HtmlBox2);
}

The demo application

Demo application loading contents to two HtmlBox controls

Limitations and workaroundsResource intensitivity

Although this asynchronous approach does keep your application fully responsive while the long-running loads are taking place, you need to be aware of that the processing power required is of course consumed by the target of your request. If the target of the request is a gateway within your application, this may be a concern, depending on the resource intensivity of the request.

Using this BeginInvoke approach will post your long-running request, and then your request is set on IO-wait-hold, until the long-running request has been handled, then a new thread is created to perform the callback to your callback procedure. This new thread is allocated from an IIS thread pool which is a different threadpool than your worker threads are allocated from, but still it is a limited resource as any other resource. In extream cases this might become a problem, and in the Other references section you will find articles on how you can have these threads allocated from a custom threadpool.


RererencesForum discussonsOther references


About the author

Related Articles

DHTML  
Title Update Author
The following code integrates google maps into Visual WebGui by creating a GoogleMap control which can be used as any Visual WebGui control.
Tags: Architects, Developers
05/Jan/2007    2007/01/05
Tags: Architects, Developers, 1. Beginner, 2. Intermediate, 3. Advanced, DHTML, v6.3, v6.4 and Later
30/Oct/2010    2010/10/30
Tags: Architects, Developers, 1. Beginner, 2. Intermediate, 3. Advanced, Integration, Pre v6.3, v6.3, v6.4 and Later
14/Nov/2010    2010/11/14
Tags: Architects, Developers, 1. Beginner, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
23/July/2010    2010/07/23
Tags: Architects, Developers, 1. Beginner, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
23/July/2010    2010/07/23
Tags: Developers, 1. Beginner, Integration, Silverlight, Pre v6.3, v6.3, 2. Intermediate, 3. Advanced
23/July/2010    2010/07/23
.NET Web, Cloud and Mobile application delivery platform | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2011 Visual WebGui®       Visual WebGui weblog on ASP.NET Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS