ASP.Net getting data from a background thread?

Hi,

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.
Xian1979Asked:
Who is Participating?
 
Xian1979Connect With a Mentor Author Commented:
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;
            T.Start();
        }
    }
    //######################################################################
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        DataTable dt = (DataTable)HttpRuntime.Cache.Get("SysInfo.GetDrives");
        if (dt != null)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
            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">
      <ContentTemplate>
         <asp:Timer ID="Timer1" runat="server" Interval="100" OnTick="Timer1_Tick" />
            <asp:GridView ID="GridView1" runat="server"/>
      </ContentTemplate>
   </asp:UpdatePanel>
</form>
 
--------------------------------------- --------------------------------------- ---------------------------------------
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();
Properties.Add("Description");
Properties.Add("Name");
Properties.Add("VolumeName");
Properties.Add("FileSystem");
Properties.Add("Size");
Properties.Add("FreeSpace");
Properties.Add("Compressed");
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 ;-)
0
 
Xian1979Author Commented:
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?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.