Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think you should reconsider your architecture altogether. The fact that you can only have 3 simultaneously connections is almost begging you to use 1 thread to generate the list of files and 3 threads to process them. Your producer thread would insert all files into a queue and the 3 consumer threads will dequeue and continue processing as items arrive in the queue. A blocking queue can significantly simplify the code. If you are using .NET 4.0 then you can take advantage of the <a href="http://msdn.microsoft.com/en-us/library/dd267312.aspx" rel="nofollow noreferrer">BlockingCollection</a> class.</p> <pre><code>public class Example { private BlockingCollection&lt;string&gt; m_Queue = new BlockingCollection&lt;string&gt;(); public void Start() { var threads = new Thread[] { new Thread(Producer), new Thread(Consumer), new Thread(Consumer), new Thread(Consumer) }; foreach (Thread thread in threads) { thread.Start(); } } private void Producer() { while (true) { Thread.Sleep(TimeSpan.FromSeconds(5)); ScheduledTask task = GetScheduledTask(); if (task != null) { foreach (string file in task.Files) { m_Queue.Add(task); } } } } private void Consumer() { // Make a connection to the resource that is assigned to this thread only. while (true) { string file = m_Queue.Take(); // Process the file. } } } </code></pre> <p>I have definitely oversimplified things in the example above, but I hope you get the general idea. Notice how this is much simpler as there is not much in the way of thread synchronization (most will be embedded in the blocking queue) and of course there is no use of <code>WaitHandle</code> objects. Obviously you would have to add in the correct mechanisms to shut down the threads gracefully, but that should be fairly easy.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload