【问题标题】:Stop long running process in webapi externally从外部停止 webapi 中长时间运行的进程
【发布时间】:2019-02-18 00:16:48
【问题描述】:

我正在使用 webclient 代码执行操作结果 LongRunningProcess,无论如何要在外部停止进程执行而不是在 iis 中停止它?

 public IHttpActionResult LongRunningProcess()
        {
            for (int i = 0; i < 5000000; i++)
            {
                Thread.Sleep(1000);
            }
            return Json("Completed");
        }



 using (WebClient wc = new WebClient())
                {
  string myParameters = "";
  wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
  string HtmlResult =await wc.UploadStringTaskAsync("http://...../LongRunningProcess", myParameters);
                }

【问题讨论】:

标签: asp.net-mvc asp.net-mvc-4 asp.net-web-api asp.net-web-api2


【解决方案1】:

LongRunningProcess 需要配合才能停止。作为如何执行此操作的示例,以下解决方案可能对您很实用:

使LongRunningProcess 定期检查基于数据库的标志。如果设置了该标志,则退出LongRunningProcess。然后,您可以执行另一个操作来设置该标志(可能在按下按钮时)。

【讨论】:

  • 我通过将值存储在静态变量中并使用另一个调用重置该变量来做到这一点
  • 当您的网站托管在多台服务器上时,这将无法正常工作。如果只是一台服务器应该没问题。 @PrincePrasad
  • 是的,请求将仅来自一台服务器。所以我实现了。
猜你喜欢
  • 2010-11-12
  • 1970-01-01
  • 2018-06-03
  • 2014-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多