using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace ch14_4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // create shared object used by threads
            SynchronizedBuffer shared = new SynchronizedBuffer();

            // Random object used by each thread
            Random random = new Random();
            // output column heads and initial buffer state
            CommonClass.output += "Operation                   Buffer    Occupied Count\n";
            CommonClass.output += "----------------------------------------------------";
            shared.DisplayState("Initial state           ");

            // create Producer and Consumer objects
            Producer producer = new Producer(shared, random);
            Consumer consumer = new Consumer(shared, random);

            // create threads for producer and consumer and set 
            // delegates for each thread
            Thread producerThread =
               new Thread(new ThreadStart(producer.Produce));
            producerThread.Name = "Producer";

            Thread consumerThread =
               new Thread(new ThreadStart(consumer.Consume));
            consumerThread.Name = "Consumer";

            // start each thread
            producerThread.Start();
           //  MessageBox.Show(SharedClass.output);
            consumerThread.Start();
            //  waiting for until program do its task 
            for (int x = 1; x < 100000000; x++) ;
            label1.Text = CommonClass.output;
        }
    }
}
