ASP.Net getting data from a background thread?

Posted on 2009-04-23
Last Modified: 2013-11-07

I have a web page that queries a computer on the network for things like, hard disk details, ram, baseboard, users, etc.. via the WMI / SNMP and binds the result to a GridView.  This works fine but the problem is that the page takes ages to load becuase it has to wait for each request to finish before it loads the page.

So along come UpdatePanels which are unfortunately asynchronous so it sitll takes as long to load.

Next come's threading which I believe is the answer but can't quite get the code right.  I can create multiple threads in the background to run each request, but i cant return the results to the page.  I imagine multple UpdatePanels which refresh every second until its designated thread has finished and then display the result and stop refreshing.  Which means some UpdatePanels will stop refreshing before others.

So how can you get the result from a background thread and display it to the web page?  Preferably the threads must run in a differnt pool to ASP.NET so it doesn't slow down the site.

Many thanks for your expertise.
Question by:Xian1979
    LVL 14

    Assisted Solution


    Author Comment

    Hi GiftsonDJohn,

    Thank you for your reply.

    How do you see this code as a solution to my problem?  I can see that it's possible to throw an exception across threads but then how would you actually get say a DataTable bound to a GridView within an UpdatePanel?

    Accepted Solution

    SOLVED!  Do I get the points :-D

    Here's what I did:

    --------------------------------------- --------------------------------------- ---------------------------------------
    In my CodeBehind I have

    using System;
    using System.Web;
    using System.Data;
    using System.Threading;
    public partial class UpdatePanel : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            StartBackgroundThread(delegate { SysInfo.GetDrives(comp, un, pw); });
            StartBackgroundThread(delegate { SysInfo.ComputerSystem(comp, un, pw); });
            StartBackgroundThread(delegate { WinInfo.Shares(comp, un, pw); });
        private void StartBackgroundThread(ThreadStart threadStart)
            if (threadStart != null)
                Thread T = new Thread(threadStart);
                T.IsBackground = true;
                T.Priority = ThreadPriority.BelowNormal;
        protected void Timer1_Tick(object sender, EventArgs e)
            DataTable dt = (DataTable)HttpRuntime.Cache.Get("SysInfo.GetDrives");
            if (dt != null)
                GridView1.DataSource = dt;
                Timer1.Enabled = false;
    --------------------------------------- --------------------------------------- ---------------------------------------
    In my page.aspx I have:

    <form id="form1" runat="server">
       <asp:ScriptManager ID="ScriptManager1" runat="server" />
       <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
             <asp:Timer ID="Timer1" runat="server" Interval="100" OnTick="Timer1_Tick" />
                <asp:GridView ID="GridView1" runat="server"/>
    --------------------------------------- --------------------------------------- ---------------------------------------
    and finally, in my Class file, I have

    using System;
    using System.Data;
    using System.Collections;
    using System.Web;
    using System.Web.Caching;
    public class SysInfo
    public SysInfo() { }
    public static void GetDrives(String Computer, String Username, String Password)
    ArrayList Properties = new ArrayList();
    DataTable dt = WMICommon.FormatIntoRows(Computer, Username, Password, Properties,
    "SELECT * FROM Win32_LogicalDisk");
    HttpRuntime.Cache.Insert("SysInfo.GetDrives", dt,
    null, DateTime.Now.Add(new TimeSpan(0, 1, 0)), Cache.NoSlidingExpiration);
    Obviously not all the code is there, but you should get the general concept.
    Hope it helps you out ;-)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
    Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
    This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
    Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now