2

I am executing the action result LongRunningProcess using webclient code, anyway to stop the process execution externally rather than stopping it in 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);
                }
Prince Prasad
  • 1,528
  • 1
  • 16
  • 20
  • 1
    If you don't hand off the process to outside IIS (for example into MSMQ) then you are going to have to do some magic with threads. – Wurd Sep 13 '18 at 09:38
  • This might give you a glimpse of the effort required if you use threads: https://learn.microsoft.com/en-us/dotnet/standard/threading/cancellation-in-managed-threads – Wurd Sep 13 '18 at 10:14
  • Check if cancellationToken suits in your scenario. More info: https://stackoverflow.com/questions/19010856/should-we-use-cancellationtoken-with-mvc-web-api-controllers – Rahul Salvi Sep 23 '18 at 16:02

1 Answers1

0

LongRunningProcess will need to cooperate to allow itself to be stopped. As an example for how to do this the following solution might be practical for you:

Make LongRunningProcess check a database based flag periodically. If that flag is set, exit LongRunningProcess. Then, you can make another action to set that flag (possibly at the press of a button).

usr
  • 168,620
  • 35
  • 240
  • 369