views:

45

answers:

2

I have a page that has 3 used controls that are dynamically added to the page. One of these use controls hits a class that pulls a record set from the database (a rather quick request) and the other 2 user controls hit a single class and return a dataset from the database. The 2 that share a single class take an average of 10 seconds to return the data. Now what I want is for a user to hit the page and see 3 loading bars and have each user control load in the background. I want them all to start loading at the same time.

I have tried to call threading on the page containing the user controls like this:

 protected void Page_Load(object sender, EventArgs e)
{       
    Thread t = new Thread(new ThreadStart(Load1));
    t.Start();
    t.IsBackground = true;
    t.Priority = ThreadPriority.Highest;
    t.Join();

    Thread s = new Thread(new ThreadStart(Load2));
    s.Start();
    s.IsBackground = true;
    s.Priority = ThreadPriority.Highest;
    s.Join();

    Thread r = new Thread(new ThreadStart(Load3));
    r.Start();
    r.IsBackground = true;
    r.Priority = ThreadPriority.Highest;
    r.Join();
}

private void Load1()
{
    Control ctrl1 = LoadControl(@"\Controls\ctrl1.ascx");
    Pnl1.Controls.Add(ctrlPreSolic);
}

private void Load2()
{
    Control ctrl2 = LoadControl(@"\Controls\ctrl2.ascx");
    Pnl2.Controls.Add(ctrlActive);
}

private void Load3()
{
    Control ctrl3 = LoadControl(@"\Controls\ctrl3.ascx");
    Pnl3.Controls.Add(ctrlNonActive);
}

however this is not working, it is always loading the first control, then the second, then the third in a line not all at the same time. Is this the wrong way to approach a web application? Can you thread a web application? I really need help with this if anyone can help me out!

A: 

Using threads like you do, is definitely not the way to load web controls asynchronously.

If you insist on your approach, all the threads should join only afterwards, else each will block the execution, and effectively eliminate any multithreading advantages you tried to gain.

leppie
How can I add them asynchronously without threading? Any examples?
EvanGWatkins
Personally I have not used it, but there are quite a few examples on MSDN, this has been present since the beginning of .NET.
leppie
A: 

Instead of using the server for this, I'd suggest using an AJAX call. You could use a third party library like jQuery and its load method, and it would fire them off as fast as possible.

Still in order, but no one call would be waiting on another, which is what I think you're trying to avoid.

This definitely feels like something that should be on the client side.

CubanX
I have created a division around each user control call and using jQuery called the load function on each division and they are still loading one at a time, how can I have them load all at the same time?
EvanGWatkins
Can we see the jQuery code you implemented please?
CubanX
how do I post code in the comment?
EvanGWatkins
Probably best to add it to original question, but if worse comes to worst, just inline right here will do
CubanX