0

I need to terminate all threads associated with the user who started it and i want to do it after user Log out. I have a function in every Controller that names thread with request path and user name:

Thread.CurrentThread.Name = $"{HttpContext.Current.Request.Path} {System.Web.HttpContext.Current.User.Identity.Name}";

How can I find them all? I already tried with Process.GetCurrentProcess().Threads but it returns ProcessThread objects that does not contains names - i need Thread objects.

1 Answers1

0

I don't think that this is how you do it. There should be not "website related threads" for a user when a site isn't "loading". Why should there be any "background threads" for the user? Where should they come from? When a user requests a webpage, the webserver loads this site and serves it to the user. Then the "thread" is done and destroyed. When the user submits a form inside the page, load another page or reloads the page, a new thread will start... and also this thread will end and be destroyed, when the page is loaded completely.

Marvin Klar
  • 1,869
  • 3
  • 14
  • 32
  • I have a few very long processes in my ApiControllers and it happens that a user on the other browser tab logs out, but the process on the server is still running (these are processes in which I expect confirmation via SignalR - backend process is waiting in loop for user confirmation or other processes that needs a lot of time to finish) – michalek92 Jun 04 '19 at 11:58
  • I would suggest to save the thread `ids` in a list/collection for each user. When a user logs out you can get the thread by it's id and kill it. Here is explained how to get the thread id https://stackoverflow.com/questions/1679243/getting-the-thread-id-from-a-thread an here how to get the thread by id: https://social.msdn.microsoft.com/Forums/en-US/e32a2fea-752f-4936-96da-0dc6945f463d/obtaining-a-thread-from-threadid?forum=csharplanguage – Marvin Klar Jun 04 '19 at 12:09
  • it was my first thought, but it seems a bit of a bad solution and I was hoping there is a better way to do it. I will try to find a different way, but thanks for answer. – michalek92 Jun 04 '19 at 12:20