[英]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: 我想到两种方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.