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

时间:2015-11-05 14:55:02

标签: c# asp.net asp.net-mvc multithreading modal-dialog

我有一个mvc Web应用程序,用户向服务器发送请求。在执行方法DoSomething(int x)时,将进行验证。所以我需要回复用户要求确认的回复。如果用户单击是,我应该继续执行DoSomething(int x)。如果用户响应否我将停止让用户进行一些更改并再次重新提交。 我试着在下面的代码中模仿我的场景:这个想法是让你理解这个问题。 现在的问题是:有没有办法使用线程? 提示:如果这是一个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);
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

简短回答:不,这在MVC中是不可能的。

更长的答案:在MVC中工作时,你需要有所不同。

我想到了两种方法:

  1. 使用JavaScript在客户端显示确认对话框,仅在用户确认他/她的选择时才转到服务器。
  2. 创建显示确认的MVC视图,如果用户确认他/她的选择,则继续。这基本上将您的流程分为两个单独的步骤。