1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Transferring graphics between pannels using semaphores and buffers in C#

Discussion in 'C#' started by Prevaron, Apr 26, 2014.

  1. #1
    I have a problem with my C#. Trying to create the airstrip with planes heading out of the hangars and on to the road to the airstrip. Each Hangar pannel is connected with closes road to them. What i am stuck with right now is connecting the roads together so each one of them will see if the next road is free and if it is it will send the plane (colorder square for now) to it and then proceed to the next one when finaly they will line up for the final panel(airstrip) and take off.

    So far they are just going down the each road they are assigned with semaphore and thread.

    using System;
    using System.Windows.Forms;
    using System.Threading;
    using System.ComponentModel;
    using System.Collections;
    using System.Data;
    using System.Drawing;
    //
    //  airfield: version 33
    //
    public class Form1 : Form
    {
    
        private Container components = null;
        private ButtonPanelThread p1, p3, p4; // the pannels that are added to the ButtonPanelThread
        private Button btn1, btn3, btn4; //buttons that are added
        private WaitPanelThread p2, p5, p6; // pannel is added ( the waiting command for the boxes) WaitPanelThread
        private Thread thread1, thread2, thread3, thread4, thread5, thread6; //threads. the threads are connected to pannels. each pannel needs to be a thread to communicate.
        private Semaphore semaphore, semaphore2, semaphore3; //one semaphore is added for the connection between the pannels. more panels more sempahores.
        private Buffer buffer, buffer2, buffer3; //the buffer connected to buttons and panels. initiates the locks and stops.
        private Thread semThread; //the thread connected with semaphore
        private Thread buffThread; // the thread connected with buffer
        private Panel pnl1, pnl2, pnl3, pnl4, pnl5, pnl6; // panels 1 2 and 3....
    
    
        public Form1()
        {
            InitializeComponent();
    
    
            semaphore = new Semaphore();
            semaphore2 = new Semaphore();
            semaphore3 = new Semaphore();
            buffer = new Buffer();
            buffer2 = new Buffer();
            buffer3 = new Buffer();
    
    
            p1 = new ButtonPanelThread(new Point(10, 30),
                                 120, true, pnl1,
                                 Color.Blue,
                                 semaphore,
                                 buffer,
                                 btn1);
    
            p2 = new WaitPanelThread(new Point(10, 10),
                                 70, true, pnl2,
                                 Color.White,
                                 semaphore,
                                 buffer);
    
            p3 = new ButtonPanelThread(new Point(10, 30),
                                 120, true, pnl3,
                                 Color.Red,
                                 semaphore2,
                                 buffer2,
                                 btn3);
    
            p4 = new ButtonPanelThread(new Point(10, 30),
                                      120, true, pnl4,
                                      Color.Yellow,
                                      semaphore3,
                                      buffer3,
                                      btn4);
    
            p5 = new WaitPanelThread(new Point(10, 10),
                                 70, true, pnl5,
                                 Color.White,
                                 semaphore2,
                                 buffer2);
    
            p6 = new WaitPanelThread(new Point(10, 10),
                                 70, true, pnl6,
                                 Color.White,
                                 semaphore3,
                                 buffer3);
    
            semThread = new Thread(new ThreadStart(semaphore.Start));
            buffThread = new Thread(new ThreadStart(buffer.Start));
            thread1 = new Thread(new ThreadStart(p1.Start));
            thread2 = new Thread(new ThreadStart(p2.Start));
            thread3 = new Thread(new ThreadStart(p3.Start));
            thread4 = new Thread(new ThreadStart(p4.Start));
            thread5 = new Thread(new ThreadStart(p5.Start));
            thread6 = new Thread(new ThreadStart(p6.Start));
    
            this.Closing += new CancelEventHandler(this.Form1_Closing);
    
            semThread.Start();
            buffThread.Start();
            thread1.Start();
            thread2.Start();
            thread3.Start();
            thread4.Start();
            thread5.Start();
            thread6.Start();
    
        }
    
    
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                    components.Dispose();
            }
            base.Dispose(disposing);
        }
    
        private void InitializeComponent()
        {
            this.Text = "Bermuda Triangle Airways";
            this.Size = new System.Drawing.Size(1000, 800); // (x, y) size of the whole window.
            this.BackColor = Color.LightGray;
            //this.BackgroundImage =Image.FromFile(@".\stone.gif");
    
            this.pnl1 = new Panel();
            this.pnl1.Location = new Point(80, 40); //(x, y) position of the panel 1
            this.pnl1.Size = new Size(30, 200); //panel size width and height.
            this.pnl1.BackColor = Color.White;
    
            this.btn1 = new Button();
            this.btn1.Size = new Size(30, 30);
            this.btn1.BackColor = Color.Pink;
            this.btn1.Location = new System.Drawing.Point(0, 0);
    
    
            this.pnl2 = new Panel();
            this.pnl2.Location = new Point(80, 240);//(x, y) position of the panel 1
            this.pnl2.Size = new Size(220, 30);
            this.pnl2.BackColor = Color.White;
            //this.pnl2.BackgroundImage =Image.FromFile(@".\stone.gif");
    
    
            this.pnl3 = new Panel();
            this.pnl3.Location = new Point(300, 40);
            this.pnl3.Size = new Size(30, 200);
            this.pnl3.BackColor = Color.White;
    
            this.btn3 = new Button();
            this.btn3.Size = new Size(30, 30);
            this.btn3.BackColor = Color.Pink;
            this.btn3.Location = new System.Drawing.Point(0, 0);
    
            this.pnl4 = new Panel();
            this.pnl4.Location = new Point(500, 40);
            this.pnl4.Size = new Size(30, 200);
            this.pnl4.BackColor = Color.White;
    
            this.btn4 = new Button();
            this.btn4.Size = new Size(30, 30);
            this.btn4.BackColor = Color.Pink;
            this.btn4.Location = new System.Drawing.Point(0, 0);
    
            this.pnl5 = new Panel();
            this.pnl5.Location = new Point(300, 240);//(x, y) position of the panel 1
            this.pnl5.Size = new Size(200, 30);
            this.pnl5.BackColor = Color.White;
    
            this.pnl6 = new Panel();
            this.pnl6.Location = new Point(500, 240);//(x, y) position of the panel 1
            this.pnl6.Size = new Size(200, 30);
            this.pnl6.BackColor = Color.White;
    
    
            this.Controls.Add(pnl1);
            this.Controls.Add(pnl2);
            this.Controls.Add(pnl3);
            this.Controls.Add(pnl4);
            this.Controls.Add(pnl5);
            this.Controls.Add(pnl6);
    
            this.pnl1.Controls.Add(btn1);
            this.pnl3.Controls.Add(btn3);
            this.pnl4.Controls.Add(btn4);
    
            // Wire Closing event.     
            this.Closing += new CancelEventHandler(this.Form1_Closing);
        }
    
        private void Form1_Closing(object sender, CancelEventArgs e)
        {
            // Environment is a System class.
            // Kill off all threads on exit.
            Environment.Exit(Environment.ExitCode);
        }
    
    
    }// end class form1
    
    public class Buffer
    {
        private Color planeColor;
        private bool empty = true;
    
        public void Read(ref Color planeColor)
        {
            lock (this)
            {
                // Check whether the buffer is empty.
                if (empty)
                    Monitor.Wait(this);
                empty = true;
                planeColor = this.planeColor;
                Monitor.Pulse(this);
            }
        }
    
        public void Write(Color planeColor)
        {
            lock (this)
            {
                // Check whether the buffer is full.
                if (!empty)
                    Monitor.Wait(this);
                empty = false;
                this.planeColor = planeColor;
                Monitor.Pulse(this);
            }
        }
    
        public void Start()
        {
        }
    
    }// end class Buffer
    
    public class Buffer2
    {
        private Color planeColor;
        private bool empty = true;
    
        public void Read(ref Color planeColor)
        {
            lock (this)
            {
                // Check whether the buffer is empty.
                if (empty)
                    Monitor.Wait(this);
                empty = true;
                planeColor = this.planeColor;
                Monitor.Pulse(this);
            }
        }
    
        public void Write(Color planeColor)
        {
            lock (this)
            {
                // Check whether the buffer is full.
                if (!empty)
                    Monitor.Wait(this);
                empty = false;
                this.planeColor = planeColor;
                Monitor.Pulse(this);
            }
        }
    
        public void Start()
        {
        }
    
    }
    
    public class Buffer3
    {
        private Color planeColor;
        private bool empty = true;
    
        public void Read(ref Color planeColor)
        {
            lock (this)
            {
                // Check whether the buffer is empty.
                if (empty)
                    Monitor.Wait(this);
                empty = true;
                planeColor = this.planeColor;
                Monitor.Pulse(this);
            }
        }
    
        public void Write(Color planeColor)
        {
            lock (this)
            {
                // Check whether the buffer is full.
                if (!empty)
                    Monitor.Wait(this);
                empty = false;
                this.planeColor = planeColor;
                Monitor.Pulse(this);
            }
        }
    
        public void Start()
        {
        }
    
    }
    public class Semaphore
    {
        private int count = 0;
    
        public void Wait()
        {
            lock (this)
            {
                while (count == 0)
                    Monitor.Wait(this);
                count = 0;
            }
        }
    
        public void Signal()
        {
            lock (this)
            {
                count = 1;
                Monitor.Pulse(this);
            }
        }
    
        public void Start()
        {
        }
    
    }// end class Semaphore
    
    
    public class Semaphore2
    {
        private int count = 0;
    
        public void Wait()
        {
            lock (this)
            {
                while (count == 0)
                    Monitor.Wait(this);
                count = 0;
            }
        }
    
        public void Signal()
        {
            lock (this)
            {
                count = 1;
                Monitor.Pulse(this);
            }
        }
    
        public void Start()
        {
        }
    
    }
    
    public class Semaphore3
    {
        private int count = 0;
    
        public void Wait()
        {
            lock (this)
            {
                while (count == 0)
                    Monitor.Wait(this);
                count = 0;
            }
        }
    
        public void Signal()
        {
            lock (this)
            {
                count = 1;
                Monitor.Pulse(this);
            }
        }
    
        public void Start()
        {
        }
    
    }
    
    public class ButtonPanelThread
    {
        private Point origin;
        private int delay;
        private Panel panel;
        private bool northSouth;
        private Color colour;
        private Point plane;
        private int xDelta;
        private int yDelta;
        private Semaphore semaphore;
        private Buffer buffer;
        private Button btn;
        private bool locked = true;
    
    
    
        public ButtonPanelThread(Point origin,
                                 int delay,
                                 bool northSouth,
                                 Panel panel,
                                 Color colour,
                                 Semaphore semaphore,
                                 Buffer buffer,
                                 Button btn)
        {
            this.origin = origin;
            this.delay = delay;
            this.northSouth = northSouth;
            this.panel = panel;
            this.colour = colour;
            this.plane = origin;
            this.panel.Paint += new PaintEventHandler(this.panel_Paint);
            this.xDelta = 0;
            this.yDelta = northSouth ? +8 : -10;
            this.semaphore = semaphore;
            this.buffer = buffer;
            this.btn = btn;
            this.btn.Click += new System.
                                  EventHandler(this.btn_Click);
    
    
        }
    
        private void btn_Click(object sender,
                               System.EventArgs e)
        {
            locked = !locked;
            this.btn.BackColor = locked ? Color.Pink : Color.LightGreen;
            lock (this)
            {
                if (!locked)
                    Monitor.Pulse(this);
            }
        }
    
        public void Start()
        {
            Color signal = Color.Red;
            Thread.Sleep(delay);
    
    
            for (int k = 1; k <= 6; k++)
            {
                this.zeroPlane();
                panel.Invalidate();
                lock (this)
                {
                    while (locked)
                    {
                        Monitor.Wait(this);
                    }
                }
                for (int i = 1; i <= 20; i++)
                {
                    this.movePlane(xDelta, yDelta);
                    Thread.Sleep(delay);
                    panel.Invalidate();
                }
                semaphore.Wait();
                buffer.Write(this.colour);
            }
            this.colour = Color.Gray;
            panel.Invalidate();
        }
    
        private void zeroPlane()
        {
            plane.X = origin.X;
            plane.Y = origin.Y;
        }
    
        private void movePlane(int xDelta, int yDelta)
        {
            plane.X += xDelta; plane.Y += yDelta;
        }
    
        private void panel_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
    
            SolidBrush brush = new SolidBrush(colour);
            g.FillRectangle(brush, plane.X, plane.Y, 10, 10);
    
            brush.Dispose();    //  Dispose graphics resources.
            g.Dispose();        // 
        }
    }// end class ButtonPanelThread
    
    public class WaitPanelThread
    {
        private Point origin;
        private int delay;
        private Panel panel;
        private bool westEast;
        private Color colour;
        private Point plane;
        private int xDelta;
        private int yDelta;
        private Semaphore semaphore;
        private Buffer buffer;
    
    
        public WaitPanelThread(Point origin,
                           int delay,
                           bool westEast,
                           Panel panel,
                           Color colour,
                           Semaphore semaphore,
                           Buffer buffer)
        {
            this.origin = origin;
            this.delay = delay;
            this.westEast = westEast;
            this.panel = panel;
            this.colour = colour;
            this.plane = origin;
            this.panel.Paint += new PaintEventHandler(this.panel_Paint);
            this.xDelta = westEast ? +10 : -10;
            this.yDelta = 0;
            this.semaphore = semaphore;
            this.buffer = buffer;
    
        }
    
        public void Start()
        {
    
            //Thread.Sleep(delay);
            this.colour = Color.White;
            for (int k = 1; k <= 12; k++)
            {
                semaphore.Signal();
                this.zeroPlane();
    
                buffer.Read(ref this.colour);
    
                for (int i = 1; i <= 20; i++)
                {
    
                    panel.Invalidate();
                    this.movePlane(xDelta, yDelta);
                    Thread.Sleep(delay);
    
                }
                this.colour = Color.White;
                panel.Invalidate();
    
    
            }
            this.colour = Color.Gray;
            panel.Invalidate();
        }
    
        private void zeroPlane()
        {
            plane.X = origin.X;
            plane.Y = origin.Y;
        }
    
        private void movePlane(int xDelta, int yDelta)
        {
            plane.X += xDelta; plane.Y += yDelta;
        }
    
        private void panel_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            SolidBrush brush = new SolidBrush(colour);
            g.FillRectangle(brush, plane.X, plane.Y, 10, 10);
            brush.Dispose();    //  Dispose graphics resources.
            g.Dispose();        // 
        }
    }// end class WaitPanelThread
    
    public class TheOne
    {
        public static void Main()//
        {
    
            Application.Run(new Form1());
        }
    }// end class TheOne
    Code (markup):

    If one of you could help me connecting the "p2" with "p5" so the graphics from "p1" the square will travel through P2 and P5 this would be great and will allow me to procede with my studies.

    Thank you all in advance i appriciate the time you spend to take a look at this not high quality work and help people.

    P.S Sorry for the english it is not my first lanugage and if something is uncleare please feel free to ask. Thank you.

    Also i do realise that this might not be the best ways of using concurent Csharp and not the up to date one but this is what i have been assigned with and do not have any other options unfortunatley. Once again sorry and Thank you.

    How i understand this is that i need to have 3 buffers and 3 semaphores for each pannel. But i do not know how to add those to each one of them and then connect it with the next one as if i try and add them it eather puts a cross in the design view over that panel or says that there is limited options for this statement. Thank you.
     
    Prevaron, Apr 26, 2014 IP