运行由 ASP.NET 网页请求触发的异步操作 [英] Running an asynchronous operation triggered by an ASP.NET web page request

查看:27
本文介绍了运行由 ASP.NET 网页请求触发的异步操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个异步操作,出于各种原因需要使用对 ASP.NET 网页的 HTTP 调用来触发该操作.当我的页面被请求时,它应该开始这个操作并立即向客户端返回一个确认.

I have an asynchronous operation that for various reasons needs to be triggered using an HTTP call to an ASP.NET web page. When my page is requested, it should start this operation and immediately return an acknowledgment to the client.

此方法也通过 WCF Web 服务公开,并且运行良好.

This method is also exposed via a WCF web service, and it works perfectly.

在我第一次尝试时,抛出了一个异常,告诉我:

On my first attempt, an exception was thrown, telling me:

Asynchronous operations are not allowed in this context.
Page starting an asynchronous operation has to have the Async
attribute set to true and an asynchronous operation can only be
started on a page prior to PreRenderComplete event.

当然,我在 @Page 指令中添加了 Async="true" 参数.现在,我没有收到错误消息,但页面在异步操作完成之前一直处于阻塞状态.

So of course I added the Async="true" parameter to the @Page directive. Now, I'm not getting an error, but the page is blocking until the Asynchronous operation completes.

如何让真正的即发即忘"页面正常工作?

How do I get a true fire-and-forget page working?

一些代码以获取更多信息.它比这更复杂一些,但我已经尝试在那里获得大致的想法.

Some code for more info. It's a bit more complicated than this, but I've tried to get the general idea in there.

public partial class SendMessagePage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string message = Request.QueryString["Message"];
        string clientId = Request.QueryString["ClientId"];

        AsyncMessageSender sender = new AsyncMessageSender(clientId, message);
        sender.Start();

        Response.Write("Success");
    }
}

AsyncMessageSender 类:

The AsyncMessageSender class:

public class AsyncMessageSender
{
    private BackgroundWorker backgroundWorker;
    private string client;
    private string msg;

    public AsyncMessageSender(string clientId, string message)
    {
        this.client = clientId;
        this.msg = message;

        // setup background thread to listen
        backgroundThread = new BackgroundWorker();
        backgroundThread.WorkerSupportsCancellation = true;
        backgroundThread.DoWork += new DoWorkEventHandler(backgroundThread_DoWork);
    }

    public void Start()
    {
        backgroundThread.RunWorkerAsync();
    }

    ...
    // after that it's pretty predictable
}

推荐答案

如果你不关心向用户返回任何东西,你可以启动一个单独的线程,或者为了快速而肮脏的方法,使用委托并异步调用它.如果您不关心在异步任务完成时通知用户,则可以忽略回调.尝试在 SomeVeryLongAction() 方法的末尾放置一个断点,您会看到它在页面已经提供后完成运行:

If you don't care about returning anything to the user, you can just fire up either a separate thread, or for a quick and dirty approach, use a delegate and invoke it asynchrnously. If you don't care about notifying the user when the async task finishes, you can ignore the callback. Try putting a breakpoint at the end of the SomeVeryLongAction() method, and you'll see that it finishes running after the page has already been served up:

private delegate void DoStuff(); //delegate for the action

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
    //create the delegate
    DoStuff myAction = new DoStuff(SomeVeryLongAction); 
    //invoke it asynchrnously, control passes to next statement
    myAction.BeginInvoke(null, null);
    Button1.Text = DateTime.Now.ToString();
}


private void SomeVeryLongAction()
{
    for (int i = 0; i < 100; i++)
    {
        //simulation of some VERY long job
        System.Threading.Thread.Sleep(100);
    }
}

这篇关于运行由 ASP.NET 网页请求触发的异步操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆