Example of Asynchronous page processing in ASP.net webforms (.NET 2.0)(ASP.net webforms (.NET 2.0) 中的异步页面处理示例)
问题描述
谁能给我提供一个简单的 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) 中的异步页面处理示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ASP.net webforms (.NET 2.0) 中的异步页面处理示例
基础教程推荐
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
