title: Troubleshoting while realising robin round
body: <p>I'm trying to create a Sheduler (for fun) but it fails. Strange is that when i'm debugging in step-over style, my programm works fine, but when i'm removing all breakpoints it freezes after printing last value. So question: why does it freezes? Second: i'm using <code>Thread.Resume</code> and <code>Thread.Suspend</code>, but they are marked as obsolete. How can i avoid it?</p>
  
  <p>Code is below:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Threading;
  
  namespace ConsoleApplication143
  {
      internal class Program
      {
          private static void Main()
          {
              var rrs = new RobinRoundSheduler(2, () =&gt;
                                                  {
                                                      for (int i = 0; i &lt; 2; i++)
                                                      {
                                                          Console.WriteLine("{0} {1}", i,
                                                                            Thread.CurrentThread.ManagedThreadId);
                                                      }
  
                                                  }) {TimeForTask = new TimeSpan(1)};
              rrs.Start();
              Console.ReadKey();
          }
      }
  
  
      internal class RobinRoundSheduler
      {
          private readonly LinkedList&lt;Thread&gt; _threads;
          public TimeSpan TimeForTask { get; set; }
  
          public RobinRoundSheduler(int taskCount, ThreadStart start)
          {
              TimeForTask = TimeSpan.FromSeconds(1);
              _threads = new LinkedList&lt;Thread&gt;();
              for (int i = 0; i &lt; taskCount; i++)
              {
                  _threads.AddLast(new Thread(start));
              }
          }
  
          public void Start()
          {
              while (_threads.Count &gt; 0)
              {
                  var list = new List&lt;Thread&gt;();
                  foreach (var thread in _threads)
                  {
                      lock (thread)
                      {
                          if (thread.ThreadState == ThreadState.Unstarted)
                              thread.Start();
                          else
                              thread.Resume();
                      }
                      thread.Join(TimeForTask);
                      lock (thread)
                      {
                          if (thread.ThreadState == ThreadState.Stopped || thread.ThreadState == ThreadState.Aborted)
                              list.Add(thread);
                          else
                          {
                              thread.Suspend();
                          }
                      }
                  }
                  list.ForEach(thread =&gt; _threads.Remove(thread));
              }
          }
      }
  }
  </code></pre>
  
  <p>it seems having a deadlock problem due to Thread.Suspend() method but i dunno another alternative to suspend a thread withoud adding checks for ManualResetEvents in calling methods. But i want to call method knows nothing about multithreading.</p>