asp.net,而第一个请求处理允许单击第二个请求

asp.net while first request processing allow second request on click(asp.net,而第一个请求处理允许单击第二个请求)
本文介绍了asp.net,而第一个请求处理允许单击第二个请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Asp.net Web Form 应用程序中,如果需要很长时间,我有生成报告按钮和取消按钮来取消报告生成过程.

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.

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

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 中是不可能的,因为会话状态会产生排他锁.

I read that Concurrent request is not possible in Asp.NET because of session state makes exclusive locks.

那么如何快速取消呢?

让我的方法 async 有助于克服会话状态排他锁问题吗?

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

推荐答案

如 另一个问题,你可以这样做

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,而第一个请求处理允许单击第二个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)