title: Sending messages to Tasks
body: <p>I am writing a program that shows the mandelbrot set depending on some conditions provided by the user. As the calculation takes long (more than 500 ms), I have decided to use more than one thread. Without any previous experience, I have managed to do it by using the System.Threading.Tasks class, which works just fine. The only thing that I don't like is that <strong>every time that the mandelbrot is generated, the threads are created and then destroyed</strong>.</p>
  
  <p>This is an example of how it works. It creates the threads (Tasks) every time that the method is called.</p>
  
  <pre><code>for (int i = 0; i &lt; maxThreads; i++) {
      int a = i;
      tasks[a] = Task.Factory.StartNew(() =&gt; generateSector(a));
  }
  </code></pre>
  
  <p>I don't know really how that affects performance, but it looks like creating and destroying threads is time expensive, and that <strong>it would be more efficient to have the threads ready and waiting for a trigger message</strong>, and when they are done go back to that waiting state. May be the following example code is useful to understand this idea.</p>
  
  <pre><code>for (int i = 0; i &lt; maxThreads; i++)
      tasks[i].sendMessage("Start"); // Tells the running thread to begin its work
  </code></pre>
  
  <p>So <strong>each thread would execute an infinite loop in which it waits</strong> until they are required to do calculations. Then, it would continue with waiting. Something like this:</p>
  
  <pre><code>// Into the method that a thread executes
  while(true) {
      Wait(); // Waits for the start signal
      calculate(); // Do some calculations
  }   // Go back to waiting
  </code></pre>
  
  <p>Would that be more efficient? Is there any way to do that?</p>