asp.net,同时处理第一个请求时允许单击第二个请求 [英] asp.net while first request processing allow second request on click

查看:113
本文介绍了asp.net,同时处理第一个请求时允许单击第二个请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Asp.net Web窗体应用程序中,如果需要很长时间,我具有生成报告"按钮和取消"按钮可以取消报告生成过程.

In Asp.net Web Form app, I have Generate Report button and a Cancel button to cancel report generation process, if it takes a long time.

当我单击生成报告"时,它会执行繁重的任务,三秒钟后,我尝试通过单击取消"按钮来取消此繁重的任务.

When I Click Generate Report it does the heavy task, after 3 seconds I try to cancel this heavy task by clicking the Cancel button.

但是,在某些时间延迟后,将调用取消"按钮的服务器端代码.

But the server side code for Cancel button click is called after some time delay.

我什至尝试在 JavaScript 中使用 window.stop()来停止页面加载并快速命中服务器代码,但是仍然存在延迟.

I even tried window.stop() in JavaScript to stop page loading and hit server code fast, but still, there is a delay.

代码:

protected void btnExportExcel_Click(object sender, EventArgs e)
{

// Doing Heavy Task to Generate Report

}


protected void btnCancel_Click(object sender, EventArgs e)
{
    if (Response.IsClientConnected)
    {

       HttpContext.Current.ApplicationInstance.CompleteRequest();

    }
}     

  <asp:Button ID="btnCancel" runat="server" Text="Cancel Request" 

  OnClientClick="return StopPageLoading();" OnClick="btnCancel_Click" />


 function StopPageLoading() {

     try
     {
       window.stop();
     } catch (exception) 
     {
       document.execCommand('Stop'); // for IE and other browsers
     }

  }

当当前请求正在处理中时,如何允许快速单击启动另一个请求?

How can I allow to start another request on click fast, while current request is in processing?

如何允许 UI 进行响应?

更新:

要重现这种情况:

  1. 我单击导出到Excel",处理需要7分钟.
  2. 在我仍在处理的同时,单击取消"按钮,还需要5分钟才能取消.

我阅读了并发由于会话状态进行互斥锁定,因此在Asp.NET中无法请求.

那么如何快速取消?

将使我的方法 async 有助于克服会话状态互斥锁问题吗?

Will making my methods async help to overcome session state exclusive locks issue ?

推荐答案

您的另一个问题,您可以通过这种方式完成

As in another question from you, you can do it this way

public partial class Contact : Page
{

    private static CancellationTokenSource tokenSource = new CancellationTokenSource();


    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected async void btnExportExcel_Click(object sender, EventArgs e)
    {
        CancellationToken cToken = tokenSource.Token;
        cToken.Register(() => cancelNotification());

        try
        {

            await Task.Run(() =>
            {
                cToken.ThrowIfCancellationRequested();

                GenerateReport(sender, cToken);

            }, cToken);
        }
        catch(OperationCanceledException)
        {

        }
    }

    private void GenerateReport(object sender, CancellationToken ct)
    {
        // Just to Simulate Export to excel

        Thread.Sleep(70000);
    }


    protected void btnCancel_Click(object sender, EventArgs e)
    {

        tokenSource.Cancel();

    }


    private static void cancelNotification()
    {
        // Do your stuff when the user is canceld the report generation

    }
}

在您的 Page.aspx

需要这样的东西

<%@ Page Async="true" EnableSessionState="false" %>

希望这对您有所帮助!

这篇关于asp.net,同时处理第一个请求时允许单击第二个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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