Please help me -- c# web application I need to load 3 user controls at the same time
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.Contro开发者_如何学Cls.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!
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.
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.
精彩评论