开发者

Add content asynchronously (threading + ajax)

开发者 https://www.devze.com 2022-12-22 15:49 出处:网络
Ok what id like to do is to load a page that displays 90% of content, and load the last 10% asynchronously.

Ok what id like to do is to load a page that displays 90% of content, and load the last 10% asynchronously.

As im rendering the page i programatically create an update panel which i pass to a thread. When this thread finishes it then updates the updatepanel on the main thread.

public void test(object parameter)
{
    Thread.Sleep(2000);
    var updpanel = (UpdatePanel)parameter;
    updpanel.ContentTemplateContainer.Controls.Add(new LiteralControl("HI"));
    updpanel.Update();
}
protected void Page_Load(object sender, EventArgs e)
{
    var th = new Thread(new ParameterizedThreadStart(test));
    var updpanel = new UpdatePan开发者_运维问答el() { UpdateMode = UpdatePanelUpdateMode.Conditional };
    ContentPlaceHolder1.Controls.Add(updpanel);
    th.Start(updpanel);
}

Failing this, in a single threaded approach, do i just keep polling the page to see if it has finished or not?


One thing to keep in mind is, even though ASP.Net development seems like it's closely related to windows development, there's a big difference: your code to handle a request, most of the time, is executed in a fraction of a second and done... In this example, once the main thread has completed, the generated page has already been sent to the requestor, leaving your secondary thread running in the background of your server, working on a page that has already been sent out.

What you'll probably need to do is generate the 90% of the page, and then send it out. On your page, you will need to use ajax to, on page load (for the client), request the other 10% from the server... You'll probably want to do the javascript with a library like jquery, and on the server end setup a web service to handle the requests.


You cannot use threading this way on the server. Your second thread will probably not complete until after the page have been processed and the request lifecycle is complete.

Depending on what you are trying to achieve: do you need to process data in parallel server-side you should look into Asynchronous Pages in ASP.Net 2.0.

Another approach would be to render the page ("90%" as you call it) and use ajax on the client to request additional data.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号