问题描述
谁能给我提供一个简单的 ASP.NET Webforms 2.0 异步页面处理示例(我使用的是 VS 2010,所以像 lambdas 这样的新语法还可以)?
Can someone provide me with a simple example of Asynchronous page processing in ASP.NET Webforms 2.0 (I'm using VS 2010, so new syntax like lambdas are ok)?
我有一些长时间运行的请求,我不想占用 IIS 线程.
I have some long running requests that I don't want tying up IIS threads.
为简单起见,假设我当前的代码如下所示:
For simplicity's sake, let's say my current code looks like this:
protected void Page_Load(object sender, EventArgs e)
{
string param1 = _txtParam1.Text;
string param2 = _txtParam2.Text;
//This takes a long time (relative to a web request)
List<MyEntity> entities = _myRepository.GetEntities(param1, param2);
//Conceptually, I would like IIS to bring up a new thread here so that I can
//display the data after it has come back.
DoStuffWithEntities(entities);
}
如何修改此代码以使其异步?假设我已经在 aspx 页面中设置了 async="true".
How can I modify this code so that it is asynchronous? Let's assume that I already set async="true" in the aspx page.
编辑
我想我知道如何获得我正在寻找的东西.我已将示例代码放在答案中 此处.请随时指出任何缺陷或可以进行的更改.
I think I figured out how to get what I'm looking for. I've put the example code in an answer here. Feel free to point out any flaws or changes that can be made.
推荐答案
我询问了 ASP.NET 团队的一些人.这是他们给我的电子邮件回复,现在是给你的.
I asked some folks on the ASP.NET team. Here's their emailed response to me, and now, to you.
代码最终要做的就是启动一个新线程并在该线程上执行委托调用.所以现在有两个线程在运行:请求线程和新线程.因此,此示例实际上比原始同步代码的性能更差.
All that code ends up doing is spinning up a new thread and performing delegate invocation on that thread. So now there are two threads running: the request thread and the new thread. Hence this sample actually has worse performance than the original synchronous code would have had.
参见 http:///www.asp.net/web-forms/tutorials/aspnet-45/using-asynchronous-methods-in-aspnet-45 有关如何在 ASP.NET 中编写和使用异步方法的示例.>
See http://www.asp.net/web-forms/tutorials/aspnet-45/using-asynchronous-methods-in-aspnet-45 for a sample on how to write and consume async methods in ASP.NET.
这篇关于ASP.net webforms (.NET 2.0) 中的异步页面处理示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!