“正在加载”异步调用的网页
我有一个简单的 ASP.NET / C# 网页。目前,为了完全呈现数据,我需要调用在后台线程上运行的代码块,并且可能需要几分钟才能完成。我已经明白了(使用页面声明上的 async 属性)来执行并在完成后返回 html。我希望它做的是允许我立即返回某种“加载页面”,然后在后台工作完成后更新该页面。现在,当后台工作正在处理时,我在页面上什么也没有得到。任何有关最佳方法或巧妙方法的想法将不胜感激!
谢谢,
西格
I have a simple web page in ASP.NET / C#. Currently to fully render the data I require calling a block of code that runs on background threads and can take multiple minutes to complete. I've got it to the point (using the async attribute on the page declaration) to execute and return fine with the html once it's done. What I'd like it to do is allow me to return immediately with a 'loading page' of some sort and then have that page be updated when the background work has been completed. Right now I get nothing on the page while the background work is being processed. Any ideas on the best way or clever way to do that would greatly be appreciated!
Thanks,
Sieg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我同意上面发表的评论。
然而,我愿意并且已经使用 ashx (处理程序)将“处理器”实现为单独的页面,因为从 javascript 调用和轮询这些非常简单,并且您只需编写流程请求事件,将内容输入到响应流中。
我很想在我的网站上向您展示一个示例,但我的主机决定在一周内停止运行我的网站,同时在其服务器上更新 .net(您可以想象我对此不是特别满意)。
本质上,就我而言,我有一个页面依赖于以下数据:
英国广播公司
谷歌
亚马逊
YouTube
其他一些随机站点。
该页面返回给用户,然后页面上的各个控件将 ajax 调用返回到服务器以获取其部分。
将加载反馈数据的标签的默认内容是一个简单的动画 gif 图像,看起来很像 flash 或 silverlight 加载圆圈。
数据返回后,gif 就会替换为服务器提供的内容。
这意味着客户端看到“正在加载”,而服务器正忙于处理客户端需要的所有内容(从其角度来看似乎是同步的)。
这是一个干净的用户体验,而且代码非常简单。
当我研究如何使用 silverlight 进行文件上传时,我被这个想法绊倒了。
基本概念:
希望这有帮助:)
I agree with the comments posted above.
I would and have however implement the "processor" as a separate page using a ashx (handler) as these are really simple to call and poll from javascript and you simply code the process request event feeding stuff in to the response stream.
I'd love to show you an example on my site but my host has decided to have a week off running my site whilst they update .net on their servers (s you can imagine im not particularly pleased with this).
Essentially though in my case I have a page that relies on data from:
The bbc
google
amazon
youtube
some other random sites.
The page is returned to the user and the individual controls on the page then make ajax calls back to the server for their parts.
The default content for the tags in which the fed back data will be loaded in to is a simple animated gif image that looks much like a flash or silverlight loading circle.
Once the data comes back the gif is replaced with the server fed content.
This means the client sees "loading" and the server is busy handling everything (seemingly syncronously from its point of view) the client needs.
It's a clean user experience and the code is really simple.
I tripped over the idea when looking at how to do file uploading using silverlight.
Basic concept:
Hope this helps :)
有很多方法可以使用 Ajax 实现您所描述的内容,但是如果您想保持非常简单,您可以在开始长时间运行的任务之前
Flush()
使用一些加载屏幕来刷新您的响应:There are many ways to implement what you describe using Ajax but if you want to keep it really simple you can simply
Flush()
your response with some loading screen before you start the long running tasks:您想要的称为 HTTP 服务器推送 或 HTTP Streaming 或 < em>长轮询。取决于它是如何完成的。
网上有很多例子,也有深入解释问题的文章。我首先要研究这些。
您还可以查看有关 HTTP Streaming 的问题...
“HTTP Streaming”(推送)AJAX 模式的跨浏览器实现
那里有一个很棒的介绍文章的链接。
或者这个带有长轮询的示例
我如何实现基本的“长轮询”?
或
https://stackoverflow.com/questions/tagged/long-polling
希望这有帮助。
What you want is called HTTP Server Push or HTTP Streaming or Long Polling. Depending on how it's done.
There are lots of examples on the web, and also articles that explain the issues in depth. I'd begin by researching those.
You can also check out this SO question on HTTP Streaming...
Cross-browser implementation of "HTTP Streaming" (push) AJAX pattern
There's a link to a great intro article in there.
Or this one with an example of Long Polling
How do I implement basic "Long Polling"?
or
https://stackoverflow.com/questions/tagged/long-polling
Hope this helps.