简体   繁体   English

单击其他按钮时停止执行按钮单击

[英]Stop execution of button click when some other button is clicked

consider the below two button click events , i want the output cancel_click event to stop the process of hellotest_click event and redirect 考虑以下两个按钮点击事件,我想输出cancel_click事件来停止hellotest_click事件和重定向的过程

  protected async void hellotest_click(object sender, EventArgs e)
    {

        await Task.Run(() => hello1());
        await Task.Run(() => hello2());          
    }

      //if i click another button , say 

    protected async void cancel_click(object sender, EventArgs e)
    {

        Response.Redirect(Request.Url.GetLeftPart(UriPartial.Authority) + "/trial/main.aspx", false);
        Context.ApplicationInstance.CompleteRequest();         
    }

//  i want the output cancel_click event to stop the process of hellotest_click event and redirect.

Unfortunatelly I don't think this is possible in general case. 不幸的是,我认为这在一般情况下是不可能的。 Imagine your project gets as big as the SO and you use a farm of many servers to handle users. 想象一下,您的项目与SO一样大,您使用许多服务器的农场来处理用户。 Different clicks might easily arrive to different servers. 不同的点击可能很容易到达不同的服务器

Any solution I can imagine is either not that simple or doesn't work correctly for example if the user has several tabs. 我能想象的任何解决方案要么不那么简单,要么不能正常工作,例如,如果用户有多个选项卡。

Probably the best thing you can do is 你可以做的最好的事情可能就是

  • introduce some service for background execution 介绍一些后台执行服务

  • introduce some registry of tasks being executed 介绍一些正在执行的任务的注册表

  • return ID of the newly created task immediately to the browser and show some progress UI 将新创建的任务的ID立即返回给浏览器并显示一些进度UI

  • regularly at some interval ask server about completion of the task 定期在某个时间间隔询问服务器完成任务

  • when user clicks cancel, pass the ID of the task to the server and ask your service to stop executing that task by ID. 当用户单击取消时,将任务的ID传递给服务器,并要求您的服务停止按ID执行该任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM