异步添加内容(线程 + ajax)

发布于 2024-08-24 21:25:43 字数 674 浏览 2 评论 0原文

好吧,id 喜欢做的是加载一个显示 90% 内容的页面,并异步加载最后 10%。

当我渲染页面时,我以编程方式创建一个更新面板,并将其传递给线程。当该线程完成后,它会更新主线程上的 updatepanel。

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 UpdatePanel() { UpdateMode = UpdatePanelUpdateMode.Conditional };
    ContentPlaceHolder1.Controls.Add(updpanel);
    th.Start(updpanel);
}

如果失败了,在单线程方法中,我是否只是继续轮询页面以查看它是否已完成?

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 UpdatePanel() { 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

永言不败 2024-08-31 21:25:43

需要记住的一件事是,尽管 ASP.Net 开发看起来与 Windows 开发密切相关,但还是有很大的区别:处理请求的代码大多数时候都是在不到一秒的时间内执行并完成的。 ...在此示例中,一旦主线程完成,生成的页面就已发送给请求者,使辅助线程在服务器后台运行,处理已发送出去的页面。

您可能需要做的是生成页面的 90%,然后将其发送出去。在您的页面上,您需要使用 ajax 在页面加载(对于客户端)时从服务器请求另外 10%...您可能需要使用像 jquery 这样的库来执行 javascript,并在服务器端设置一个 Web 服务来处理请求。

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.

计㈡愣 2024-08-31 21:25:43

您不能在服务器上以这种方式使用线程。在处理页面并且请求生命周期完成之后,您的第二个线程可能不会完成。

根据您想要实现的目标:您是否需要在并行服务器端处理数据,您应该查看 ASP.Net 2.0 中的异步页面

另一种方法是渲染页面(您称之为“90%”)并在客户端上使用 ajax 来请求其他数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文