0

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.

Community
  • 1
  • 1
DDiVita
  • 4,225
  • 5
  • 63
  • 117

1 Answers1

2

You can increment an integer in thread-safe mode with Interlocked.Increment(ref toYourGlobalMember).

About cancellation, maybe you can give a try to the TPL and Cancellation Tokens.

margabit
  • 2,924
  • 18
  • 24
  • No problem @DDiVita, hope you can solve your cancelling threads issue . I've been doing some research with TPL, if you choose that way maybe I can help you more. – margabit Aug 29 '12 at 13:06
  • i looked at TPL and think it might be a better fit, do you know how I could achieve the same code using TPL? I am still researching how this works, cause I have not used it yet. – DDiVita Aug 30 '12 at 11:38
  • With TPL you can use Parallel.ForEach() method to create new Tasks. It needs a Collection and a delegate that will do the task for each item in your collection. TPL will create new threads if needed, it uses the ThreadPool, so you don't have to worry about creating new threads. If you want to limit the concurrency you can set the MaxDegreeOfParallelism in System.Threading.Tasks.ParallelOptions. You can see an example here: http://msdn.microsoft.com/en-us/library/dd460720.aspx – margabit Aug 30 '12 at 12:26
  • About cancelling your pending Tasks, here's an example http://blogs.msdn.com/b/pfxteam/archive/2009/06/22/9791840.aspx – margabit Aug 30 '12 at 12:29