私の見解では、Start()アクションを呼び出すボタンとStop()を呼び出すボタンの2つのボタンがあります。コントローラは次のようになります。MVCで非同期アクションをキャンセルできません
public class TestController : AsyncController
{
public static CancellationTokenSource cts = new CancellationTokenSource();
public void StartAsync()
{
cts = new CancellationTokenSource();
CancellationToken cancellationToken = cts.Token;
AsyncManager.OutstandingOperations.Increment();
AsyncManager.Parameters["task"] = Task.Factory.StartNew(() =>
{
try
{
while() {
if (cancellationToken.IsCancellationRequested)
break;
// Do stuff
}
}
finally
{
AsyncManager.OutstandingOperations.Decrement();
}
}, cancellationToken);
}
public ActionResult StartCompleted(Task task)
{
try
{
task.Wait();
return Json(new { success = true }, JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
return Json(new { success = false }, JsonRequestBehavior.AllowGet);
}
}
public void StopAsync()
{
cts.Cancel();
}
public ActionResult StopCompleted()
{
return Json(new { success = true }, JsonRequestBehavior.AllowGet);
}
}
しかし、私が停止ボタンをクリックしたとき、停止アクションは開始終了後にのみ呼び出されます。どのように私はアクションを停止するはずですか?ブックProの.NETパフォーマンスから
IIS Expressの問題になりました:https://github.com/aspnet/Mvc/issues/5239#issuecomment-323567394 –