7 Replies - 609 Views - Last Post: 18 November 2014 - 10:13 PM Rate Topic: ****- 1 Votes

#1 Prevaron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-April 14

Concurent C sharp panel connection with semaphores and buffers.

Posted 26 April 2014 - 03:02 PM

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, btn8; //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, pnl7, pnl8, pnl9; // 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.pnl7 = new Panel();
        this.pnl7.Location = new Point(701, 240);//(x, y) position of the panel 1
        this.pnl7.Size = new Size(30, 200);
        this.pnl7.BackColor = Color.White;

        this.pnl8 = new Panel();
        this.pnl8.Location = new Point(80, 441);//(x, y) position of the panel 1
        this.pnl8.Size = new Size(690, 30);
        this.pnl8.BackColor = Color.White;
        //this.pnl2.BackgroundImage =Image.FromFile(@".\stone.gif"); (Landing zome image)

        this.btn8 = new Button();
        this.btn8.Size = new Size(30, 30);
        this.btn8.BackColor = Color.Pink;
        this.btn8.Location = new System.Drawing.Point(660,0);

        this.pnl9 = new Panel();
        this.pnl9.Location = new Point(80, 271);//(x, y) position of the panel 1
        this.pnl9.Size = new Size(30, 169);
        this.pnl9.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.Controls.Add(pnl7);
        this.Controls.Add(pnl8);
        this.Controls.Add(pnl9);

        this.pnl1.Controls.Add(btn1);
        this.pnl3.Controls.Add(btn3);
        this.pnl4.Controls.Add(btn4);
        this.pnl8.Controls.Add(btn8);


        // 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



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.

One again I am sorry that this is the way that i want to do it unfortunatley it must be so. The most flexible this can be is by adding an array for semaphores and buffer ( do not know how to do that eaither).

Is This A Good Question/Topic? 0
  • +

Replies To: Concurent C sharp panel connection with semaphores and buffers.

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3667
  • View blog
  • Posts: 11,499
  • Joined: 05-May 12

Re: Concurent C sharp panel connection with semaphores and buffers.

Posted 26 April 2014 - 06:22 PM

It would have been more helpful if you had named your controls and panels with more descriptive names instead of p1, p2, p3, btn1, btn2, btn3, etc. I'm having a really hard time keeping track of which object is which. Also can you tell us which panels are hangars and which ones are roads, and which one is the air strip?

As a quick aside, you should only be updating your UI, on the main UI thread, not each one of the threads that you are running.

Another aside is your mixing of your simulation logic with your UI. This is a terrible way to do things, but as you said, you want to keep things that way, it's your headache to manage.

And now with your specific problem, you say that you are using the semaphores to connect the panels that are supposed to be connected. I'll take your word for it since I can't make heads or tails of your code because of the poor names, as well as the mixing of UI and simulation logic. Can you do a Snoke case of just one hangar, one road, and one air strip to make sure everything works?
Was This Post Helpful? 1
  • +
  • -

#3 Prevaron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-April 14

Re: Concurent C sharp panel connection with semaphores and buffers.

Posted 27 April 2014 - 04:39 AM

Thank you for your advise. I am refactoring the code right now making it more readable, also adding the arrays for buffers and semaphores so it will make it more tidier. I will upload it here in a littlebit as i still have some problems with it. Thank you so much for the reply.

This post has been edited by andrewsw: 27 April 2014 - 06:16 AM
Reason for edit:: Removed previous quote

Was This Post Helpful? 0
  • +
  • -

#4 Prevaron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-April 14

Re: Concurent C sharp panel connection with semaphores and buffers.

Posted 27 April 2014 - 05:34 AM

Updated my code. now it is actually readable ) The top pannels are joined together and are now creating a travel path to the point of the bottom pannel lane (the landing zone). What i want it to do is to reach that panel and then travel throug it and take off. The problems i am having with are : how to make the planes (squares) go the other direction (left to right) and how to disappear at the end instead of deadlocking.

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 Buttonpanel1, Buttonpanel2, Buttonpanel3; // the pannels that are added to the ButtonPanelThread 
    private Button btn1, btn3, btn4; //buttons that are added
    private WaitPanelThread Waitpanel1, Waitpanel2, Waitpanel3, Waitpanel4; // pannel is added ( the waiting command for the boxes) WaitPanelThread
    private Thread thread1, thread2, thread3, thread4, thread5, thread6, thread7; //threads. the threads are connected to pannels. each pannel needs to be a thread to communicate.
    private Panel pnl1, pnl2, pnl3, pnl4, pnl5, pnl6, pnl7, pnl8, pnl9; // panels 1 2 and 3....

    private static int counter = 10;
    private Semaphore[] semArray = new Semaphore[counter];
    private Buffer[] buffArray = new Buffer[counter];
    private Thread[] semThreadArray = new Thread[counter];
    private Thread[] buffThreadArray = new Thread[counter];


    

    public Form1()
    {
        InitializeComponent();



        for (int i = 0; i < counter; i++)
        {
            semArray[i] = new Semaphore();
            buffArray[i] = new Buffer();
        }

        semArray[0] = new Semaphore();
        semArray[1] = new Semaphore();
        semArray[2] = new Semaphore();
        semArray[3] = new Semaphore();

        buffArray[0] = new Buffer();
        buffArray[1] = new Buffer();
        buffArray[2] = new Buffer();
        buffArray[3] = new Buffer();

        Plane plane1 = new Plane(Color.Blue);
        Plane plane2 = new Plane(Color.Red);
        Plane plane3 = new Plane(Color.Yellow);
        Plane plane4 = new Plane(Color.Purple);

         Buttonpanel1 = new ButtonPanelThread(new Point(10, 30),//top left button pannel
                              120, true, pnl1,
                              plane1,
                               semArray[0], semArray[7],
                               buffArray[0], buffArray[7],
                              btn1);

        Buttonpanel2 = new ButtonPanelThread(new Point(10, 30),//top middle button pannel
                             120, true, pnl3,
                             plane2,
                             semArray[1], semArray[8],
                             buffArray[1], buffArray[8],
                             btn3);

        Buttonpanel3 = new ButtonPanelThread(new Point(10, 30),//top right button pannel
                                  120, true, pnl4,
                                  plane3,
                                  semArray[2], semArray[9],
                                  buffArray[2], buffArray[9],
                                  btn4);

        Waitpanel1 = new WaitPanelThread(new Point(10, 10), //top left waitpanel
                                         70, true, 10, 20, pnl2,
                                         new Plane(Color.Green),
                                         semArray[0], semArray[1], semArray[7],
                                         buffArray[0], buffArray[1], buffArray[7], true);

        Waitpanel2 = new WaitPanelThread(new Point(10, 10),//top middle waitpanel
                                         70, true, 10, 18, pnl5,
                                         new Plane(Color.Green),
                                         semArray[1], semArray[2], semArray[8],
                                         buffArray[1], buffArray[2], buffArray[8], true);


        Waitpanel3 = new WaitPanelThread(new Point(10, 10),
                                         70, true, 10, 18, pnl6,
                                         new Plane(Color.Green),
                                         semArray[2], semArray[3], semArray[9],
                                         buffArray[2], buffArray[3], buffArray[9], true);
        

        Waitpanel4= new WaitPanelThread(new Point(10, 10),
                                         70, false, 10, 18, pnl7,
                                         new Plane(Color.Green),
                                         semArray[3], semArray[4], semArray[6],
                                         buffArray[3], buffArray[4], buffArray[6], true);


        

        




        for (int i = 0; i < counter; i++)
        {
            semThreadArray[i] = new Thread(new ThreadStart(semArray[i].Start));
            buffThreadArray[i] = new Thread(new ThreadStart(buffArray[i].Start));
        }

        thread1 = new Thread(new ThreadStart(Buttonpanel1.Start));
        thread2 = new Thread(new ThreadStart(Waitpanel1.Start));
        thread3 = new Thread(new ThreadStart(Buttonpanel2.Start));
        thread4 = new Thread(new ThreadStart(Buttonpanel3.Start));
        thread5 = new Thread(new ThreadStart(Waitpanel2.Start));
        thread6 = new Thread(new ThreadStart(Waitpanel3.Start));
        thread7 = new Thread(new ThreadStart(Waitpanel4.Start));

        this.Closing += new CancelEventHandler(this.Form1_Closing);

        for (int i = 0; i < counter; i++)
        {
            semThreadArray[i].Start();
            buffThreadArray[i].Start();
        }

        
        thread1.Start();
        thread2.Start();
        thread3.Start();
        thread4.Start();
        thread5.Start();
        thread6.Start();
        thread7.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.pnl7 = new Panel();
        this.pnl7.Location = new Point(701, 240);//(x, y) position of the panel 1
        this.pnl7.Size = new Size(30, 200);
        this.pnl7.BackColor = Color.White;

        this.pnl8 = new Panel();
        this.pnl8.Location = new Point(80, 441);//(x, y) position of the panel 1
        this.pnl8.Size = new Size(651, 30);
        this.pnl8.BackColor = Color.White;
        //this.pnl2.BackgroundImage =Image.FromFile(@".\stone.gif"); (Landing zome image)

        //this.btn8 = new Button();
        //this.btn8.Size = new Size(30, 30);
        //this.btn8.BackColor = Color.Pink;
        //this.btn8.Location = new System.Drawing.Point(660,0);

        this.pnl9 = new Panel();
        this.pnl9.Location = new Point(80, 271);//(x, y) position of the panel 1
        this.pnl9.Size = new Size(30, 169);
        this.pnl9.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.Controls.Add(pnl7);
        this.Controls.Add(pnl8);
        this.Controls.Add(pnl9);

        this.pnl1.Controls.Add(btn1);
        this.pnl3.Controls.Add(btn3);
        this.pnl4.Controls.Add(btn4);
        //this.pnl8.Controls.Add(btn8);


        // 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 Plane thePlane;
    private bool empty = true;

    public void Read(ref Plane plane)
    {
        lock (this)
        {
            // Check whether the buffer is empty.
            if (empty)
                Monitor.Wait(this);
            empty = true;
            plane = thePlane;
            Monitor.Pulse(this);
        }
    }

    public void Write(Plane plane)
    {
        lock (this)
        {
            // Check whether the buffer is full.
            if (!empty)
                Monitor.Wait(this);
            empty = false;
            thePlane = plane;
            Monitor.Pulse(this);
        }
    }

    public void Start()
    {
    }

}// end class Buffer


public class Semaphore
{
    public 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 ButtonPanelThread
{
    private Point origin;
    private int delay;
    private Panel panel;
    private bool northSouth;
    private Plane thePlane;
    private Point planeOrogin;
    private int xDelta;
    private int yDelta;
    private Semaphore semaphore, semaphore2;
    private Buffer buffer, buffer2;
    private Button btn;
    private bool locked = true;
    private bool westEast;



    public ButtonPanelThread(Point origin,
                             int delay,
                             bool northSouth,
                             Panel panel,
                             Plane plane,
                             Semaphore semaphore, Semaphore semaphore2,
                             Buffer buffer, Buffer buffer2,
                             Button btn)
    {
        this.origin = origin;
        this.delay = delay;
        this.northSouth = northSouth;
        this.panel = panel;
        this.thePlane = plane;
        this.planeOrogin = origin;
        this.panel.Paint += new PaintEventHandler(this.panel_Paint);
        this.xDelta = 0;
        this.yDelta = northSouth ? +8 : -10;
        this.semaphore = semaphore;
        this.semaphore2 = semaphore2;
        this.buffer = buffer;
        this.buffer2 = buffer2;
        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);

        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.thePlane);

        planeOrogin.Y = -100;
        //thePlane.PlaneColor = Color.Transparent;
        panel.Invalidate();
        for (int l = 1; l <= 99; l++)
        {
            semaphore2.Signal();
            buffer2.Read(ref thePlane);
            planeOrogin.Y = 160;

            if (this.thePlane.ToHub)
            {
                this.thePlane.ToHub = false;
                westEast = false;
                for (int i = 1; i <= 20; i++)
                {
                    this.movePlane(xDelta, -yDelta);
                    Thread.Sleep(delay);
                    panel.Invalidate();
                }
            }

            lock (this)
            {
                while (btn.BackColor == Color.Pink)
                    Monitor.Wait(this);
            }
            westEast = true;
            if (btn.BackColor == Color.LightGreen)
            {
                for (int i = 1; i <= 20; i++)
                {
                    this.movePlane(xDelta, yDelta);
                    Thread.Sleep(delay);
                    panel.Invalidate();
                }
                semaphore.Wait();
                buffer.Write(this.thePlane);
                planeOrogin.Y = -100;
                //thePlane.PlaneColor = Color.Transparent;
                panel.Invalidate();
            }
        }
    }

    private void zeroPlane()
    {
        planeOrogin.X = origin.X;
        planeOrogin.Y = origin.Y;
    }

    private void movePlane(int xDelta, int yDelta)
    {
        planeOrogin.X += xDelta; planeOrogin.Y += yDelta;
    }

    private void panel_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;

        SolidBrush brush = new SolidBrush(thePlane.PlaneColor);
        g.FillRectangle(brush, planeOrogin.X, planeOrogin.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, panel1;
    private bool westEast;
    private Plane plane, colour1;
    private Point planeO;
    private int xDelta;
    private int yDelta;
    private Semaphore hubSemaphore, pnlSemaphore, rtnSemaphore;
    private Buffer hubBuffer, pnlBuffer, rtnBuffer;
    private int distance = 23;
    private Color colour;
    private bool bothWays;
    private int speed;

    public WaitPanelThread(Point origin,
                       int delay,
                       bool westEast, int speed, int distance,
                       Panel panel,
                       Plane plane,
                       Semaphore semaphore, Semaphore semaphore1, Semaphore rtnSemaphore,
                       Buffer buffer, Buffer buffer1, Buffer rtnBuffer, bool bothWays)
    {
        this.origin = origin;
        this.delay = delay;
        this.westEast = westEast;
        this.panel = panel;
        this.plane = plane;
        this.planeO = origin;
        this.bothWays = bothWays;
        this.speed = speed;
        this.panel.Paint += new PaintEventHandler(this.panel_Paint);
        this.distance = distance;
        this.rtnBuffer = rtnBuffer;
        this.rtnSemaphore = rtnSemaphore;
        if (westEast == true)
        {
            this.xDelta = speed;
            this.yDelta = 0;
        }
        else
        {
            this.xDelta = 0;
            this.yDelta = speed;
        }
        this.hubSemaphore = semaphore;
        this.pnlSemaphore = semaphore1;
        this.hubBuffer = buffer;
        this.pnlBuffer = buffer1;
    }

    public void Start()
    {
        planeO = new Point(-100, -100);
        for (int k = 1; k <= 99; k++)
        {
            hubSemaphore.Signal();
            hubBuffer.Read(ref this.plane);
            this.zeroPlane();
            panel.Invalidate();

            for (int i = 1; i <= distance; i++)
            {
                panel.Invalidate();
                this.movePlane(xDelta, yDelta);
                Thread.Sleep(delay);
            }


            if (this.plane.ToHub && bothWays && rtnSemaphore.count == 1)
            {
                rtnSemaphore.Wait();
                rtnBuffer.Write(this.plane);
                planeO = new Point(-100, -100);
                panel.Invalidate();

            }
            else
            {
                pnlSemaphore.Wait();
                Plane temp = this.plane;
                pnlBuffer.Write(temp);
                planeO = new Point(-100, -100);
                panel.Invalidate();
            }

        }
        panel.Invalidate();
    }

    private void zeroPlane()
    {
        planeO.X = origin.X;
        planeO.Y = origin.Y;
    }

    private void movePlane(int xDelta, int yDelta)
    {
        planeO.X += xDelta; planeO.Y += yDelta;
    }


    private void panel_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        SolidBrush brush = new SolidBrush(plane.PlaneColor);
        g.FillRectangle(brush, planeO.X, planeO.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

public class Plane
{
    private bool toHub = false;
    private Color planeColor;

    public Plane(Color c)
    {
        planeColor = c;
    }

    public bool ToHub
    {
        get { return toHub; }
        set { toHub = value; }
    }

    public Color PlaneColor
    {
        get { return planeColor; }
        set { planeColor = value; }
    }
}



Hope you can help me out. What i think that i need to do is create another waitpannel class which will be special for the landing zone and change the direction of the object flow inside it. And then add the function to get rid of the objects when they reac end so the next object can proceed. Unfortunatley i am unable to do that. If you can give me a helping hand that would be very great. Once again thank you for advice and reply.

Just a picture of how it looks and what i want of it with some infos .

Posted Image

Sorry not (left to right but right to left).

This post has been edited by andrewsw: 27 April 2014 - 06:16 AM
Reason for edit:: Removed unnecessary quote, just press Reply

Was This Post Helpful? 0
  • +
  • -

#5 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 362
  • View blog
  • Posts: 1,539
  • Joined: 08-April 09

Re: Concurent C sharp panel connection with semaphores and buffers.

Posted 27 April 2014 - 06:06 AM

I thought you will name your controls like this, hangar1, hangar2, airstrip1, airstrip2 etc ...
Was This Post Helpful? 0
  • +
  • -

#6 Prevaron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-April 14

Re: Concurent C sharp panel connection with semaphores and buffers.

Posted 27 April 2014 - 07:54 AM

Wel with time and lots of reading through the C i have done what i was requered. Thank you for advice of tidying up the code it helped me a lot. Now i can carry on with studies. This thread can be closed now and if you need more info on the solution just messege me thanks.
Was This Post Helpful? 0
  • +
  • -

#7 steve6661  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-November 14

Re: Concurent C sharp panel connection with semaphores and buffers.

Posted 15 November 2014 - 03:48 PM

View PostPrevaron, on 27 April 2014 - 07:54 AM, said:

Wel with time and lots of reading through the C i have done what i was requered. Thank you for advice of tidying up the code it helped me a lot. Now i can carry on with studies. This thread can be closed now and if you need more info on the solution just messege me thanks.


Hi Prevaron I seem to be having the same problem that you ran into. How did you go about fixing the issue of getting the plane to jump from , p1 to p2 to p5?

Thanks for any help :)
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3667
  • View blog
  • Posts: 11,499
  • Joined: 05-May 12

Re: Concurent C sharp panel connection with semaphores and buffers.

Posted 18 November 2014 - 10:13 PM

Prevaron has not logged in since April 2014 after his problem has been solved. You should probably work with Cloud91 who seems to be also tackling this same problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1