using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;

namespace ThreadLibrary
{
   public class FileIO
   {
      public void WriteToFile()
      {
         try
         {         
            Monitor.Enter(this);
            {
               FileStream fs = new FileStream("sync.txt", FileMode.Append);

               StreamWriter sw = new StreamWriter(fs);

               for (int i = 1; i < 100000; i++)
               {
                  sw.WriteLine(Thread.CurrentThread.Name);
               }

               sw.Flush();
               sw.Close();
               fs.Close();

               Console.WriteLine(Thread.CurrentThread.Name + " fertig!");
            }
         }
         catch (IOException e)
         {
            Console.WriteLine(e.ToString());
         }
         finally
         {
            //abschließende Arbeiten
            Monitor.Exit(this);
         }
      }
   }
}