无法在按钮单击时立即中断 while 循环 c# mvc [英] Unable to break while loop immediately on button click c# mvc

查看:25
本文介绍了无法在按钮单击时立即中断 while 循环 c# mvc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 mvc 操作方法,其中 while 循环正在运行我想在另一个按钮单击时停止该 while 循环,因此我维护了一个标志并将其设置为 false 以中断循环.

I have mvc action method where while loop is running I want to stop that while loop on another button click so I have maintain a flag and set it to false to break loop.

bool flag = true;

public async Demo()
{
while(flag == true)
{
do something...
}
}

现在单击按钮,我正在调用一种操作方法来停止 while 循环 -

now on button click I'm calling one action method to stop the while loop -

    public ActionResult StopLoop()  
    {  
        flag = false;
        Return View("Index")
    }  

但是点击按钮后点击这个StopLoop需要将近一分钟的时间.

But it is taking almost a minute time to hit this StopLoop after click on button.

为什么?可能都在同一个 UI 页面上并共享同一个 homecontroller我能做些什么?

why ? may be all are on same UI page and sharing same homecontroller any trick i can do ?

当循环开始时,我在一个按钮请求上有 2 个按钮,我想要另一个按钮单击请求循环停止.

i have 2 buttons on one button request while loop start and i want on another button click request loop stop.

推荐答案

如果您说一个请求启动了 while 循环,而您希望另一个请求停止它,那么这对于 ASP.NET MVC 来说是不可能的它是无国籍的.返回视图后,您的 while 循环将不再运行.在您的情况下, StopLoop 方法看起来只需要一分钟.可能真正发生的情况是您的 while 循环可能无休止地运行,IIS 将其杀死,然后处理停止循环请求.

If you're saying that one request starts the while loop, and you want another request to stop it, then that's not possible with a ASP.NET MVC as it is stateless. Once the view is returned your while loop is no longer running. In your case, it only looks like it's taking a minute for the StopLoop method to get hit. What's probably really happening is that your while loop is probably running endlessly, IIS kills it, and then the stop loop request is processed.

这篇关于无法在按钮单击时立即中断 while 循环 c# mvc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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