?
Solved

ASP.Net getting data from a background thread?

Posted on 2009-04-23
3
Medium Priority
?
589 Views
Last Modified: 2013-11-07
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.
0
Comment
Question by:Xian1979
  • 2
3 Comments
 
LVL 14

Assisted Solution

by:GiftsonDJohn
GiftsonDJohn earned 80 total points
ID: 24218402
0
 

Author Comment

by:Xian1979
ID: 24218687
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
 

Accepted Solution

by:
Xian1979 earned 0 total points
ID: 24220582
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
While opting for any web-to-print solution, you need to discuss with your team and some of your end users and know their opinions about your decisions. In this article we list down some questions you need to ask yourself.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

839 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