I have mvc application
which is hosted on azure webapp
I have simple start button and stop button.
When you click start button it send message to iot hub
When you click stop button it stop sending messages.
I achieve this using bool flag values true and false.
Now issue is when I click stop button it set flag false but unable to reflect flag value false into while loop ?
public class HomeController : Controller
{
bool flag;
static Thread mythread;
[HttpPost]
public async Task<ActionResult> Start()
{
flag = true;
if (messageType.Contains("demo1"))
{
mythread = new Thread(async () => await ProcessStart("data1",flag));
thread.Start();
}
}
private static async Task ProcessStart(string message,bool flag)
{
while (flag)
{
await deviceClient.SendEventAsync(message);
}
}
[HttpPost]
public ActionResult Stop()
{
mythread.Abort(); // still unable to kill started thread :(
flag = false;
return View("Index");
}
but after stop click also flag value in while loop is true :( how ? I can not use static here as it will false all flag variable value gloably.
I wonder why can not i track thread id and save somewhere like in hidden field or temp and kill that same thread when i click on stop button ?