title: Time Slicing Between Five Threads In C#
body: <p>Here's a description of what the program should do. The program should create a file and five threads to write in that file...</p>
  
  <p>The first thread should write from 1 to 5 into that file.
  The second thread should write from 1 to 10.
  The third thread should write from 1 to 15.
  The fourth thread should write from 1 to 20.
  The fifth thread should write from 1 to 25.</p>
  
  <p>Moreover, an algorithm should be implemented to make each thread print 2 numbers and stops. the next thread should print two numbers and stop. and so on until all the threads finish printing their numbers.
  Here's the code I've developed so far...</p>
  
  <pre><code>using System;
  using System.IO;
  using System.Threading;
  using System.Collections;
  using System.Linq;
  using System.Text;
  
  namespace ConsoleApplication1
  {
      public static class OSAssignment
      {
          // First Thread Tasks...
          static void FirstThreadTasks(StreamWriter WritingBuffer)
          {
              for (int i = 1; i &lt;= 5; i++)
              {
                  if (i % 2 == 0)
                  {
                      Console.WriteLine("[Thread1] " + i);
                      Thread.Sleep(i);
                  }
  
                  else
                  {
                      Console.WriteLine("[Thread1] " + i);
                  }
              }
          }
  
          // Second Thread Tasks...
          static void SecondThreadTasks(StreamWriter WritingBuffer)
          {
              for (int i = 1; i &lt;= 10; i++)
              {
                  if (i % 2 == 0)
                  {
                      if (i == 10)
                          Console.WriteLine("[Thread2] " + i);
  
                      else
                      {
                          Console.WriteLine("[Thread2] " + i);
                          Thread.Sleep(i);
                      }
                  }
  
                  else
                  {
                      Console.WriteLine("[Thread2] " + i);
                  }
              }
          }
  
          // Third Thread Tasks..
          static void ThirdThreadTasks(StreamWriter WritingBuffer)
          {
              for (int i = 1; i &lt;= 15; i++)
              {
                  if (i % 2 == 0)
                  {
                      Console.WriteLine("[Thread3] " + i);
                      Thread.Sleep(i);
                  }
  
                  else
                  {
                      Console.WriteLine("[Thread3] " + i);
                  }
              }
          }
  
          // Fourth Thread Tasks...
          static void FourthThreadTasks(StreamWriter WritingBuffer)
          {
              for (int i = 1; i &lt;= 20; i++)
              {
                  if (i % 2 == 0)
                  {
                      if (i == 20)
                          Console.WriteLine("[Thread4] " + i);
                      else
                      {
                          Console.WriteLine("[Thread4] " + i);
                          Thread.Sleep(i);
                      }
                  }
  
                  else
                  {
                      Console.WriteLine("[Thread4] " + i);
                  }
  
              }
          }
  
          // Fifth Thread Tasks...
          static void FifthThreadTasks(StreamWriter WritingBuffer)
          {
              for (int i = 1; i &lt;= 25; i++)
              {
                  if (i % 2 == 0)
                  {
                      Console.WriteLine("[Thread5] " + i);
                      Thread.Sleep(i);
                  }
  
                  else
                  {
                      Console.WriteLine("[Thread5] " + i);
                  }
  
              }
          }
  
          // Main Function...
          static void Main(string[] args)
          {
              FileStream File = new FileStream("output.txt", FileMode.Create, FileAccess.Write, FileShare.Write);
              StreamWriter Writer = new StreamWriter(File);
              Thread T1 = new Thread(() =&gt; FirstThreadTasks(Writer));
              Thread T2 = new Thread(() =&gt; SecondThreadTasks(Writer));
              Thread T3 = new Thread(() =&gt; ThirdThreadTasks(Writer));
              Thread T4 = new Thread(() =&gt; FourthThreadTasks(Writer));
              Thread T5 = new Thread(() =&gt; FifthThreadTasks(Writer));
              Console.WriteLine("Initiating Jobs...");
              T1.Start();
              T2.Start();
              T3.Start();
              T4.Start();
              T5.Start();
              Writer.Flush();
              Writer.Close();
              File.Close();
          }
      }
  }
  </code></pre>
  
  <p>Here's the problems I'm facing...</p>
  
  <ol>
  <li><p>I cannot figure out how to make the 5 threads write into the same file at the same time even with making <code>FileShare.Write</code>. So I simply decided to write to console for time being and to develop the algorithm and see how it behaves first in console.</p></li>
  <li><p>Each time I run the program, the output is slightly different from previous. It always happen that a thread prints only one of it's numbers in a specific iteration and continues to output the second number after another thread finishes its current iteration.</p></li>
  <li><p>I've a got a question that might be somehow offtrack. If I removed the <code>Console.WriteLine("Initiating Jobs...");</code> from the main method, the algorithm won't behave like I mentioned in Point 2. I really can't figure out why.</p></li>
  </ol>