首页 > c# > 请帮帮我 - c#web应用程序我需要同时加载3个用户控件

请帮帮我 - c#web应用程序我需要同时加载3个用户控件 (Please help me — c# web application I need to load 3 user controls at the same time)

问题

我有一个页面有3个使用的控件,动态添加到页面。其中一个使用控件命中一个从数据库中提取记录集的类(一个相当快速的请求),另外两个用户控件命中一个类并从数据库返回一个数据集。共享单个类的2平均需要10秒才能返回数据。现在我想要的是让用户点击页面并看到3个加载栏并让每个用户在后台控制负载。我希望他们都能同时开始加载。

我试图在包含用户控件的页面上调用线程,如下所示:

 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);
}

但是这不起作用,它总是加载第一个控件,然后是第二个控件,然后是第三个控件,而不是同时加载第三个控件。这是接近Web应用程序的错误方法吗?你可以线程化一个Web应用程序?如果有人能帮助我,我真的需要帮助!

解决方法

像你一样使用线程绝对不是异步加载Web控件的方法。

如果你坚持你的方法,所有的线程应该只在之后加入,否则每个线程都会阻止执行,并有效地消除你试图获得的任何多线程优势。

问题

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!

解决方法

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.

相似信息