如何暂停方法执行,并在客户端响应后恢复
本文关键字:客户端 响应 恢复 执行 何暂停 暂停 方法 | 更新日期: 2023-09-27 18:20:37
我有一个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);
}
}
}
简短回答:不,这在MVC中是不可能的。
长话短说:在MVC中工作时,您需要有一些不同的想法。
想到两种方法:
- 使用JavaScript在客户端显示确认对话框,只有在用户确认其选择时才转到服务器
- 创建一个显示确认的MVC视图,如果用户确认了他/她的选择,则继续。这基本上将你的流程分解为两个独立的步骤