I have a service I created that runs multiple threads. I don't need to communicate with each thread, individually, but rather all of them at one time. I found this article, which allows communication via a handler. I am using WCF as my service endpoint and wondering if I can communicate with that rather than a handler. Here is some sample code as to what I am doing in the service:
public class ThreadCoordinator
{
private int _numberOfThreads = 10;
private List<Thread> _threads;
public ThreadCoordinator()
{
_threads = new List<Thread>();
}
private void StartThreads()
{
for (int t = 0; t < _numberOfThreads; t++)
{
var st = new TheThread();
var thread = new Thread(new ThreadStart(st.Run));
_threads.Add(thread);
thread.Start();
}
}
public void RunThreads()
{
try
{
StartThreads();
}
finally
{
WaitForAllThreads();
CloseAllConnections();
}
}
private void WaitForAllThreads()
{
foreach (Thread t in _threads)
{
t.Join();
}
}
private void CloseAllConnections()
{
//do some stuff to clean up resources
}
}
internal class TheThread
{
public void Run()
{
//Do The work and update a global variable
foreach(var person in somePersonList)
{
//do some work on that person
_someGlobalIntegerMember ++;
}
}
}
I would like a global variable that keeps track of how much data is getting processed by all the threads. So something that keeps getting updated as a each thread is processing data. What's more, I would like the ability to pause all the threads from a command on the client side. It doesn't really matter if I perform an ajax request or form submit from MVC. This article illustrates how to pause one thread, but I am not sure if it can be applied to multiple threads.