控制器:
已更新
public ActionResult generateDoc()
{
thread = new Thread(new ThreadStart(threadSleep));
thread.Start();
return Json(true);
}
public void threadSleep()
{
Thread.Sleep(60000);
Interlocked.Exchange(ref isThreadComplete, 1);
thread.Abort();
}
public ActionResult callThreadSleep()
{
temp = isThreadComplete;
long thread_comp = Interlocked.Read(ref temp);
Interlocked.Exchange(ref isThreadComplete, 0);
return Json(thread_comp);
}
//CANCEL GENERATION OF DOCUMENT
public ActionResult cancelDocumentGen()
{
try
{
thread.Abort();
Interlocked.Exchange(ref isThreadComplete, 0);
}
catch (Exception ex)
{
//Thread.ResetAbort();
}
return Json("");
}
**UPDATE**
function doc_creation() {
$.ajax(
{
type: "POST",
url: '@Url.Action("generateDoc")',
dataType: "json",
mtype: "post",
async: true,
beforeSend: function () {
},
success: function (data) {
$("#docGenModal").show();
threadComplete = setInterval(function () {
console.log("called");
checkDocGen();
}, 1000);
}
});
}
function checkDocGen() {
$.ajax(
{
type: "POST",
url: '@Url.Action("callThreadSleep")',
dataType: "json",
mtype: "post",
async: true,
success: function (data) {
if (data == 1) {
console.log("hide " + data);
clearInterval(threadComplete);
$("#docGenModal").hide();
}
}
});
}
//CANCEL DOCUMENT GENERATION
function cancelDocGen() {
$.ajax(
{
type: "POST",
url: '@Url.Action("cancelDocumentGen")',
dataType: "json",
mtype: "post",
async: true,
success: function (data) {
alert("successfully cancelled");
}
});
}
点击按钮我调用function checkDocGen()
调用callThreadSleep
方法然后点击cancle我调用了cancelDocumentGen()
但是在中止后再次启动...有什么建议或想法吗?我可以这样中止吗? ?..我google了很多次,但所有的例子都很难理解我