【问题标题】:How to Pause a method execution , and resume after client response?如何暂停方法执行,并在客户端响应后恢复?
【发布时间】:2015-11-05 14:55:02
【问题描述】:

我有一个 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);
            }
        }

    }

【问题讨论】:

  • 为什么不在 javascript 中进行验证/确认,然后再提交到服务器?
  • 在非常正常的情况下是的。但我正在处理一个糟糕的 web 设计中的特殊应用程序。(最初在 windows 中)。
  • “非常正常的情况。我正在处理一个特殊的应用程序应用程序” 对不起,我不明白你在说什么.
  • 我们通过保持所有后端不受影响将 Windows 窗体应用程序转换为 MVC
  • 不幸的是,您将不得不触摸后端,在 MVC 中根本没有“暂停”的好方法,因为如果用户在您离开时(或关闭浏览器)会怎样?暂停?如果你使用了一个线程,你将有一个资源坐在那里永远等待。它需要分成两步过程,首先是确认,然后是操作,或者在 javascript 中进行确认。

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


【解决方案1】:

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

更长的答案:在 MVC 中工作时,您需要有一些不同的想法。

想到了两种方法:

  1. 使用 JavaScript 在客户端显示确认对话框,只有当用户确认他/她的选择时才转到服务器。
  2. 创建一个显示确认信息的 MVC 视图,如果用户确认了他/她的选择,则继续。这实际上将您的流程分为两个独立的步骤。

【讨论】:

  • 仅将逻辑改回 JS 需要做很多工作。有很多地方我们有相同的场景。将此返回给客户几乎是从头开始(管理层在 2 年前不支持)
  • 这不是一个简单的 MVC,因为表单仍然驻留在后端 @Ronald Zarīts
猜你喜欢
  • 2021-12-06
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多