Introduction

In the article, Enable Multiple Asynchronous Postbacks at the Same Time[^], Vinay Lakhanpal[^] talked about scheduling asynchronous postbacks in a FIFO manner (First In First Out). In this article, we are going to schedule these post-backs based on given priority. So urgent postbacks would be processed first.

Background

This article is based on ideas discussed in Enable Multiple Asynchronous Postbacks at the Same Time[^] by Vinay Lakhanpal[^].

The Implementation

First, we are going to create a custom control. This would be useful for reuseability. So, we extend from class Control and implement the IScriptControl Interface. This interface would access an embedded script resource through its GetScriptReferences() method.

Then we define the JavaScript classes:

  • EventReference: which represents a pair of event target and event argument of the postback.
  • Node: which represents a node to be pushed into the queue.
  • ReadyQueue: which represents the queue in which the post-back event references will be arranged.
  • and lastly AsyncScedular: which is the main component that handles the PageRequestMaanager initializeRequest and endRequest events.

The Queue

The idea of the queue is to arrange the nodes based on their priority which is one of the type Priority values: Real Time, High, Above Normal, Normal, Below Normal, Low, so the highest priority would be served first.

The queue consists of two pointers, head and tail, pointing to an array of pointers (Nodes); one points to the first node and the other points to the last. Each node has a pointer to point to the following node and the last node points to nothing (null). A node also has an instance of class EventReference to hold the postback and a priority value.

queue

Once we push an event reference in the queue with a given priority, we first create a node with the given values, we determine the position where to place the node based on the priority, we cut the connection between the two nodes, then we set the connections to pass through our node.

Using the Code

To use the AsyncScedular, we first need to tag to the markup file in a common place like a master page:

<cc1:AsyncScedular ID="AsyncScedular1" runat="server"  />

Then we can call it from the content page through the GetCurrent() method, and we just set priority to the controls through SetAsyncControlPriority() method.

Dim AsyncScedular1 As AjaxServerControl1.AsyncScedular = 
		AjaxServerControl1.AsyncScedular.GetCurrent(Me)
AsyncScedular1.LogMethod = "WriteLog"

AsyncScedular1.SetAsyncControlPriority(Button1, AjaxServerControl1.Priority.High)
AsyncScedular1.SetAsyncControlPriority(Button2, AjaxServerControl1.Priority.AboveNormal)
AsyncScedular1.SetAsyncControlPriority(Button3, AjaxServerControl1.Priority.Normal)
AsyncScedular1.SetAsyncControlPriority(Button4, AjaxServerControl1.Priority.Low)
AsyncScedular1.SetAsyncControlPriority(Button5, AjaxServerControl1.Priority.BelowNormal)
AsyncScedular1.SetAsyncControlPriority(Button6, AjaxServerControl1.Priority.RealTime)

History

  • Friday May 27, 2011: First version
推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"