Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version
Go to top

COMET (or Reverse AJAX) based Grid Control for ASP.NET Web Applications - Scalable, High Performance and Low Latency Grid Control

, 1 Apr 2009
A COMET/Reverse Ajax based Web Grid Control, which can be used in ASP.NET web application. This control posts the updates to the client based on Server side event(s) thereby reducing network round trips.
GridControlCometAjax.zip
GridControlCometAjax
bin
Release
GridControlCometAjax.dll
GridControlCometAjax.pdb
GridControlCometAjax.zip
MultiMap.dll
MultiMap.pdb
Properties
Test
App_Data
bin
GridControlCometAjax.dll
MultiMap.dll
Test.dll
Test.pdb
GridCtrl
GridControlCometAjax.dll
GridControlCometAjax.zip
MultiMap.dll
Img
bg.JPG
Properties
TestGridControl.csproj.user
Perf.zip
Test.zip
GridControlCometAjax.dll
MultiMap.dll
Test.dll
Test.pdb
GridControlCometAjax.dll
GridControlCometAjax.zip
MultiMap.dll
bg.JPG
TestGridControl.csproj.user
// Copyright (c) 2000, 2002 - 2012 Bharath K A
//
// CREATED: Sunday March 29 11:08:13 CST 2009 by Bharath K A
//
// LAST CHANGED:
//
// AUTHOR: Bharath K A
//
// Copyright 2005 by Bharath K A
// All rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Threading;


namespace BK.Util
{
    internal class GridAsynchHandler : IHttpAsyncHandler
    {
        private static object lockObj;
        private static MultimapBK<string, ResponseDetails> clientDetails;
        private static List<WaitHandle> listOfWaitHandles;
        private static List<string> listOfClientIds;
        private static AutoResetEvent eventToReInit;
        private static bool KeepRunning;
        private static Thread ProcessingThread;


        static GridAsynchHandler()
        {
            lockObj = new object();
            clientDetails = new MultimapBK<string, ResponseDetails>();
            eventToReInit = new AutoResetEvent(false);
            listOfWaitHandles = new List<WaitHandle>();
            listOfClientIds = new List<string>();
            listOfWaitHandles.Add(eventToReInit);
            listOfClientIds.Add("SELF");
            KeepRunning = true;
            ProcessingThread = new Thread(new ThreadStart(ProcessUpdates));
            ProcessingThread.Start();
        }

        internal static void RegisterControl(GridControlCometAjax gridToRegister)
        {
            lock (lockObj)
            {
                if (null == clientDetails.GetFirstItem(gridToRegister.ClientID))
                {
                    clientDetails.Add(gridToRegister.ClientID, new ResponseDetails(gridToRegister));
                    listOfClientIds.Add(gridToRegister.ClientID);
                    listOfWaitHandles.Add(gridToRegister.UpdateEvent);
                    eventToReInit.Set();

                }
                // else, It is already registered
            }
        }

        internal static void ProcessUpdates()
        {
            int WaitNumber = 0;
            int TimeoutUpdate = 1;
            StringBuilder xmlComposer = new StringBuilder();

            while(KeepRunning)
            {
                xmlComposer.Length = 0;
                WaitHandle[] waitHandles = new WaitHandle[listOfWaitHandles.Count];
                for (int Counter = 0; Counter < listOfWaitHandles.Count; Counter++)
                {
                    waitHandles[Counter] = listOfWaitHandles[Counter];
                }

                WaitNumber = AutoResetEvent.WaitAny(waitHandles, GridControlConstants.WAIT_TIMEOUT,false);

                if (WaitNumber == AutoResetEvent.WaitTimeout)
                {
                    WaitNumber = TimeoutUpdate;
                    TimeoutUpdate++;
                    if (TimeoutUpdate >= listOfWaitHandles.Count)
                    {
                        TimeoutUpdate = 1;
                    }
                }

                if (WaitNumber == 0)
                {
                    continue;
                }
                else
                {
                    ResponseDetails respDet = clientDetails.GetFirstItem(listOfClientIds[WaitNumber]);

                    xmlComposer.AppendFormat(GridControlConstants.RESP_XML_HDR, respDet.GridCtrlAjaxInstance.DynRowCount, respDet.GridCtrlAjaxInstance.DynColCount);
                    xmlComposer.Append(GridControlConstants.RESP_XML_TBL_DATA_START);

                    for (int RowItem = 0; RowItem < respDet.GridCtrlAjaxInstance.DynRowCount; RowItem++)
                    {
                        for (int ColItem = 0; ColItem < respDet.GridCtrlAjaxInstance.DynColCount; ColItem++)
                        {
                            xmlComposer.AppendFormat(GridControlConstants.RESP_XML_BODY_DATA, respDet.GridCtrlAjaxInstance.DynGetContent(RowItem, ColItem));
                        }
                    }
                    xmlComposer.Append(GridControlConstants.RESP_XML_TBL_DATA_END);
                    xmlComposer.Append(GridControlConstants.RESP_XML_TBL_CLR_START);
                    for (int RowItem = 0; RowItem < respDet.GridCtrlAjaxInstance.DynRowCount; RowItem++)
                    {
                        for (int ColItem = 0; ColItem < respDet.GridCtrlAjaxInstance.DynColCount; ColItem++)
                        {
                            xmlComposer.AppendFormat(GridControlConstants.RESP_XML_BODY_COLOR, System.Drawing.ColorTranslator.ToHtml(respDet.GridCtrlAjaxInstance.DynGetColor(RowItem, ColItem)));
                        }
                    }

                    xmlComposer.Append(GridControlConstants.RESP_XML_TBL_CLR_END);
                    xmlComposer.Append(GridControlConstants.RESP_XML_TAIL);

                    lock (lockObj)
                    {
                        respDet.XmlFeed = xmlComposer.ToString();
                        respDet.UpdateRequired = true;
                    }

                    for (int WorkerCounter = 0; WorkerCounter < GridControlConstants.NUMBER_OF_THREADS; WorkerCounter++)
                    {
                        if (respDet.UpdateRequired)
                        {
                            ThreadPool.QueueUserWorkItem(new WaitCallback(WorkerThreadProc), respDet);
                        }
                        else
                        {
                            break;
                        }
                    }

                }
            }

        }

        public static void WorkerThreadProc(object ThreadParam)
        {
            ResponseDetails respDetails = (ResponseDetails)ThreadParam;

            while (true)
            {
                GridAsynchResult asyncResult = respDetails.RemoveClient();
                
                if (asyncResult == null)
                {
                    break;
                }
                else if (asyncResult.Status == ClientStatus.Updated)
                {
                    respDetails.UpdateRequired = false;
                    respDetails.AddClient(asyncResult);
                    break;
                }
                asyncResult.SetCompleted();
            }
        }

        public IAsyncResult BeginProcessRequest(HttpContext Context, AsyncCallback Callback, object ExtraData)
        {
            string ClientId = Context.ApplicationInstance.Request.QueryString[0];
            ResponseDetails respDet = clientDetails.GetFirstItem(ClientId);

            GridAsynchResult asyncResultToAdd = new GridAsynchResult(respDet, Context, Callback, ClientStatus.Updated);
            respDet.AddClient(asyncResultToAdd);

            return asyncResultToAdd;
        }

        public void EndProcessRequest(IAsyncResult result)
        {
            try
            {
                GridAsynchResult asynchResult = (GridAsynchResult)result;
                lock (lockObj)
                {
                    asynchResult.ClientContext.Response.ContentType = "text/xml";
                    asynchResult.ClientContext.Response.Write(asynchResult.RespDetails.XmlFeed);
                }
            }
            catch (Exception)
            {

            }
        }

        public void ProcessRequest(HttpContext Context)
        {
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }


    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Bharath K A
Software Developer (Senior)
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 1 Apr 2009
Article Copyright 2009 by Bharath K A
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid