AutoResetEvent WaitOneとSetの使用例


        /// <summary>
        ///  100 
        /// </summary>
        private const int maxLineNumber = 100;

        /// <summary>
        ///  
        /// </summary>
        private const string Path = @"D:\1.txt";

        static void Main(string[] args)
        {
            AutoResetEvent autoReset = new AutoResetEvent(false);

            if (!File.Exists(Path))
            {
                FileStream fs = File.Create(Path);
                fs.Close();
            }
            //// 
            Thread readThread =
            new Thread(new ThreadStart(() =>
            {
                for (int i = 0; i < maxLineNumber; i++)
                {
///// Set
                    autoReset.WaitOne();
                    StreamReader sr = File.OpenText(Path);
                    Console.WriteLine(sr.ReadToEnd());
                    sr.Close();
                }
            }));
            readThread.Start();

            //// , 
            for (int i = 0; i < maxLineNumber; i++)
            {
                File.WriteAllText(Path, i.ToString() + "\r
"); autoReset.Set(); Thread.Sleep(1000); } Console.ReadKey(); }