在C#中阻止多线程应用程序

时间:2016-10-05 10:22:20

标签: c# multithreading file-io

我在C#编写这个非常简单的应用程序:

using System;
using System.IO;
using System.Threading;
using System.Collections.Generic;
namespace ProvaEvent
{
    class Program
    {
        static void Main(string[] args)
        {
            Box box = new Box();

            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
            sw.AutoFlush = true;
            Console.SetOut(sw);

            AutoResetEvent are = new AutoResetEvent(false);

            WorkerWaiter workerObject = new WorkerWaiter(are,box);
            Thread workerThread_1 = new Thread(workerObject.DoWork);
            workerThread_1.Start();

            WorkerSignal workerObject_2 = new WorkerSignal(are,box);
            Thread workerThread_2 = new Thread(workerObject_2.DoWork);
            workerThread_2.Start();

            workerThread_1.Join();
        }
    }


    public class WorkerWaiter
    {
        private AutoResetEvent _wake_me_up;
        private Box box;

        public WorkerWaiter(AutoResetEvent wake_me_up, Box box)
        {
            this._wake_me_up = wake_me_up;
            this.box = box;
        }

        // This method will be called when the thread is started.
        public void DoWork()
        {
            while (true)
            {
                Console.WriteLine("     [WorkerWaiter] Waiting for stuff");
                Console.Out.Flush();

                this._wake_me_up.WaitOne();

                while (box._box_list.Count != 0)
                {
                    int n = box._box_list[0];
                    box._box_list.RemoveAt(0);
                    Console.WriteLine("     [WorkerWaiter] Uhm, stuff to do, { box[0]= " + n + " } let's work hard!");
                    Console.Out.Flush();

                    Thread.Sleep(10000); // do heavy stuff!

                    Console.WriteLine("     [WorkerWaiter] [+++++++++++++]Stuff finished[+++++++++++++]");
                    Console.Out.Flush();
                }
            }
        }
    }


    public class WorkerSignal
    {
        private AutoResetEvent _wake_him_up;
        private Box box;
        private int a = 10;

        public WorkerSignal(AutoResetEvent wake_him_up, Box box)
        {
            this._wake_him_up = wake_him_up;
            this.box = box;
        }

        // This method will be called when the thread is started.
        public void DoWork()
        {
            while (true)
            {
                Console.WriteLine("[WorkerSignal] Setting the Event!");
                Console.Out.Flush();

                _wake_him_up.Set();

                lock (box._box_list)
                {
                    int n = new Random().Next(10000);
                    Console.WriteLine("{" + n + "} --> box[]");
                    Console.Out.Flush();

                    box._box_list.Add(n);
                }

                Thread.Sleep(2000);
                a--;
                if (a == 0)
                {
                    Console.WriteLine("[WorkerSignal] Sleeping for a long time now");
                    Console.Out.Flush();

                    Thread.Sleep(120000); // 2 min
                    a = 10;
                    Console.WriteLine("[WorkerSignal] Uhm, let's signal some stuff!");
                    Console.Out.Flush();
                }
            }
        }
    }


    public class Box
    {
        public List<int> _box_list;

        public Box()
        {
            _box_list = new List<int>();
        }
    }
}

我不明白为什么在控制台上运行输出几分钟后(似乎整体执行)暂停,直到我按ENTER键...我认为它与Console.WriteLine和我有关已经添加了所有Console.Out.Flush(),但这种奇怪的行为仍然存在......

0 个答案:

没有答案