I am encoding an application that runs many threads in the background, which should report this to the main thread so that it can update the table in the interface. In the past, workflows were the usual separate classes (named Citizen) that I ran from the main thread using something like
new Thread(new ThreadStart(citizen.ProcessActions)).Start();
where the ProcessActions function was the main function that did all the background work. Before starting a thread, I would register event handlers so that Citizen threads can write / report some things to the interface. Usually there are dozens of these Citizen streams (about 50), and they are quite large classes - each of them has its own HTTP client and browses the Internet.
Is this a good way to manage threads? Probably not, to be honest; I am sure that the threads do not exit gracefully - as soon as the ProcessActions function is completed, I delete the event handlers, and this - memory usage continues to grow with each new Citizen.
What would be the best way to manage the many (50+) threads that you often have to communicate with? . I suggest that I donβt have to worry about thread safety for Citizen variables as I would not access them from threads other than my own thread.
source share