1

想象一下,我有一些代码要从文件的开头到结尾读取,如下所示:

while(sw.readline != null)
{

}

我希望这是完全异步的。当从 IHTTPAsyncHandler 派生时,这段代码会去哪里,获取每一行内容的代码会去哪里?

4

1 回答 1

-2

我最近在我的 ASP.NET 页面中添加了一些异步进程,以便在用户等待结果时允许一些长时间运行的进程发生。我发现 IHTTPAsyncHandler 很不合适。它所做的只是允许您在页面开始处理时启动一个新线程。您仍然必须创建自己的线程并创建 AsyncRequestResult。

相反,我最终只是在我的代码隐藏中使用了一个普通线程,更简洁:

using System;
using System.Web;
using System.Threading;

namespace Temp {
    public partial class thread : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            Thread thread = new Thread(new ThreadStart(myAsyncProcess));
            thread.Start();
        }

        private void myAsyncProcess() {
            // Long running process
        }
    }
}
于 2009-01-09T23:06:37.217 回答