Below is the code to send a graphic through 3 panels from one panel to the next in order, panels 1 then 2 then 3, which is easy enough with 2 panels but when you add 3,4,5 panels this is where the problem starts the object should pass from left to right from panel to panel but instead after the first panel it runs in both the second and the third panel at the same time, and not in order from panel 1,2,3 instead i get 1, 2 and 3.. how do you determine the order of the panels for the object to travel...
using System; using System.Windows.Forms; using System.Threading; using System.ComponentModel; using System.Collections; using System.Data; using System.Drawing; public class Form1 : Form { private Container components = null; private ButtonPanelThread p1; private Button btn1; private WaitPanelThread p2, p3; private Thread thread1, thread2, thread3; private Semaphore semaphore; private Buffer buffer; private Thread semThread; private Thread buffThread; private Panel pnl1, pnl2, pnl3; public Form1() { InitializeComponent();
semaphore = new Semaphore(); buffer = new Buffer(); p1 = new ButtonPanelThread(new Point(40, 10), 120, true, pnl1, Color.Blue, semaphore, buffer, btn1); p2 = new WaitPanelThread(new Point(5, 10), 200, true, pnl2, Color.White, semaphore, buffer); p3 = new WaitPanelThread(new Point(5, 10), 200, true, pnl3, Color.White, semaphore, buffer); 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)); this.Closing += new CancelEventHandler(this.Form1_Closing); semThread.Start(); buffThread.Start(); thread1.Start(); thread2.Start(); thread3.Start(); } protected override void Dispose(bool disposing) { if (disposing) { if (components != null) components.Dispose(); } base.Dispose(disposing); } private void InitializeComponent() { this.pnl1 = new System.Windows.Forms.Panel(); this.btn1 = new System.Windows.Forms.Button(); this.pnl2 = new System.Windows.Forms.Panel(); this.pnl3 = new System.Windows.Forms.Panel(); this.pnl1.SuspendLayout(); this.SuspendLayout(); // // pnl1 // this.pnl1.BackColor = System.Drawing.Color.White; this.pnl1.Controls.Add(this.btn1); this.pnl1.Location = new System.Drawing.Point(29, 200); this.pnl1.Name = "pnl1"; this.pnl1.Size = new System.Drawing.Size(260, 30); this.pnl1.TabIndex = 0; // // btn1 // this.btn1.BackColor = System.Drawing.Color.Pink; this.btn1.Location = new System.Drawing.Point(0, 0); this.btn1.Name = "btn1"; this.btn1.Size = new System.Drawing.Size(30, 30); this.btn1.TabIndex = 0; this.btn1.UseVisualStyleBackColor = false; // // pnl2 // this.pnl2.BackColor = System.Drawing.Color.White; this.pnl2.Location = new System.Drawing.Point(295, 200); this.pnl2.Name = "pnl2"; this.pnl2.Size = new System.Drawing.Size(260, 30); this.pnl2.TabIndex = 1; // // pnl3 // this.pnl3.BackColor = System.Drawing.Color.White; this.pnl3.Location = new System.Drawing.Point(561, 200); this.pnl3.Name = "pnl3"; this.pnl3.Size = new System.Drawing.Size(260, 30); this.pnl3.TabIndex = 2; // // Form1 // this.BackColor = System.Drawing.Color.LightGray; this.ClientSize = new System.Drawing.Size(861, 462); this.Controls.Add(this.pnl1); this.Controls.Add(this.pnl2); this.Controls.Add(this.pnl3); this.Name = "Form1"; this.Text = "Test"; this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing); this.pnl1.ResumeLayout(false); this.ResumeLayout(false); } 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 objectColor; private bool empty = true; public void Read(ref Color objectColor) { lock (this) { // Check whether the buffer is empty. if (empty) Monitor.Wait(this); empty = true; objectColor = this.objectColor; Monitor.Pulse(this); } } public void Write(Color objectColor) { lock (this) { // Check whether the buffer is full. if (!empty) Monitor.Wait(this); empty = false; this.objectColor = objectColor; Monitor.Pulse(this); } } public void Start() { } }// end class Buffer 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 ButtonPanelThread { private Point origin; private int delay; private Panel panel; private bool westEast; private Color colour; private Point obj; 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 westEast, Panel panel,Color colour, Semaphore semaphore, Buffer buffer, Button btn) { this.origin = origin; this.delay = delay; this.westEast = westEast; this.panel = panel; this.colour = colour; this.obj = origin; this.panel.Paint += new PaintEventHandler(this.panel_Paint); this.xDelta = westEast ? +10 : -10; this.yDelta = 0; 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 <= 1; k++) { this.zeroObject(); panel.Invalidate(); lock (this) { while (locked) { Monitor.Wait(this); } } for (int i = 1; i <= 20; i++) { this.moveObject(xDelta, yDelta); Thread.Sleep(delay); panel.Invalidate(); } semaphore.Wait(); buffer.Write(this.colour); } this.colour = Color.Gray; panel.Invalidate(); } private void zeroObject() { obj.X = origin.X; obj.Y = origin.Y; } private void moveObject(int xDelta, int yDelta) { obj.X += xDelta; obj.Y += yDelta; } private void panel_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; SolidBrush brush = new SolidBrush(colour); g.FillRectangle(brush, obj.X, obj.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 obj; 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.obj = 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 <= 10; k++) { semaphore.Signal(); this.zeroObject(); buffer.Read(ref this.colour); for (int i = 1; i <= 23; i++) { panel.Invalidate(); this.moveObject(xDelta, yDelta); Thread.Sleep(delay); } this.colour = Color.White; panel.Invalidate(); } this.colour = Color.Gray; panel.Invalidate(); } private void zeroObject() { obj.X = origin.X; obj.Y = origin.Y; } private void moveObject(int xDelta, int yDelta) { obj.X += xDelta; obj.Y += yDelta; } private void panel_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; SolidBrush brush = new SolidBrush(colour); g.FillRectangle(brush, obj.X, obj.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]
Question
Sarkhan Vol
Below is the code to send a graphic through 3 panels from one panel to the next in order, panels 1 then 2 then 3, which is easy enough with 2 panels but when you add 3,4,5 panels this is where the problem starts the object should pass from left to right from panel to panel but instead after the first panel it runs in both the second and the third panel at the same time, and not in order from panel 1,2,3 instead i get 1, 2 and 3.. how do you determine the order of the panels for the object to travel...
Link to comment
Share on other sites
13 answers to this question
Recommended Posts