简体   繁体   English

如何暂停方法执行,并在客户端响应后恢复?

[英]How to Pause a method execution , and resume after client response?

I have a mvc web application in where user send request to server. 我在用户向服务器发送请求的地方有一个mvc Web应用程序。 On execution of method DoSomething(int x) , a validation occurs. 执行方法DoSomething(int x)时,将进行验证。 So i need to send back a response on the user asking for confirmation. 因此,我需要向用户发送回覆,要求确认。 If the user click Yes i should continue the execution of DoSomething(int x). 如果用户单击“是”,我应该继续执行DoSomething(int x)。 If user response No i will stop for the user to do some changes and resubmit again. 如果用户回答“否”,我将停止让用户进行一些更改并再次重新提交。 I have try to mimic my scenario in the code below: The idea is for you to understand the issue. 我尝试在下面的代码中模仿我的场景:这个想法是让您理解这个问题的。 Now the question: Is there a way to use thread? 现在的问题是:有没有一种使用线程的方法? hint:If this was a windows form it is easy Showdialog will take care of it. 提示:如果这是Windows窗体,Showdialog将很容易处理。

    public void DoSomething(int x)
    {
        // do other thing  here
        //...
        if (x > 0)
        {
            //Notify user for confirmation
            EventNotification.ModalBox("X is out of the Decision", "Do you want to proceed?");
            //httprequest will fire based on user response
            //###PAUSE HERE ####
            //wait for user response
        }

        //###RESUME HERE ####
        //Continu here  if confirmation  is YES
        x = x + 23;
        //save to database
        EventNotification.CloseBox("good");

    }

    public class EventNotification
    {
        public event System.EventHandler CloseEvent;
        public event System.EventHandler ModalBoxEvent;

        public void ModalBox(string text, string YesorNoConfirmation)
        {//raise event to open modal box 
            MyMsgBoxText = text;
            MyMsgBoxTitle = YesorNoConfirmation;
            if (ModalBoxEvent != null)
            {
                ModalBoxEvent(this, System.EventArgs.Empty);
            }
        }
        public void CloseBox(string text)
        {

            if (CloseEvent != null)
            {
                CloseEvent(this, System.EventArgs.Empty);
            }
        }

    }

Short answer: No, this is not possible in MVC. 简短答案:不,这在MVC中是不可能的。

Longer answer: you need to think a bit differently when working in MVC. 更长的答案:在MVC中工作时,您需要有所不同。

Two approaches come to mind: 我想到两种方法:

  1. Display the confirmation dialog on the client-side with JavaScript and go to the server only if the user confirms his/her choice. 在带有JavaScript的客户端上显示确认对话框,并且仅在用户确认其选择后才转到服务器。
  2. Create a MVC view that displays the confirmation, and if the user confirms his/her choice, continue. 创建一个显示确认的MVC视图,如果用户确认他/她的选择,则继续。 This essentially breaks up your flow into two separate steps. 这实际上将您的流程分为两个单独的步骤。

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

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