Drawing Shapes using Threading
This is simple multi-threading program that draws circles and rectangles. Each shape is handle by individual thread that created every time you press start button. Using sleep method on each thread we can change the speed of each shape. 
I have used VS.NET to implement this program. Double click on Thread_Example.Zip and extract all the files and folders to a folder in C drive. 
其中,基类Shapes.cs:
代码如下:
 using System;
using System; using System.Drawing;
using System.Drawing; using System.Threading;
using System.Threading;


 namespace ThreadTester
namespace ThreadTester {
{ public abstract class Shapes
    public abstract class Shapes {
    { protected int  xVal;
        protected int  xVal; protected int yVal;
        protected int yVal; protected Color color;
        protected Color color; protected int width;
        protected int width;  protected int height;
        protected int height; protected int directionY = 1;
        protected int directionY = 1; protected int directionX = 1;
        protected int directionX = 1; protected Form1 frm1;
        protected Form1 frm1; protected int speed;
        protected int speed; 
             public Shapes()
        public Shapes() {  }
        {  }
 public abstract void paint(Graphics g);
        public abstract void paint(Graphics g); 
             public void CheckCoordinates()
        public void CheckCoordinates() {
        { if((frm1.panel1.Size.Height-20 < yVal) || (yVal <= 0))
            if((frm1.panel1.Size.Height-20 < yVal) || (yVal <= 0)) directionY = directionY * (-1);
                directionY = directionY * (-1);    
 if((frm1.panel1.Size.Width-20 < xVal) || (xVal <= 0))
            if((frm1.panel1.Size.Width-20 < xVal) || (xVal <= 0)) directionX = directionX * (-1);
                directionX = directionX * (-1); }
        } }
    } }
}

继承Shapes的2个继承类:Rectangle和Circle类定义如下:
 using System;
using System; using System.Drawing;
using System.Drawing; using System.Threading;
using System.Threading;


 namespace ThreadTester
namespace ThreadTester {
{ public class Rectangle : Shapes
    public class Rectangle : Shapes {
    { public Rectangle(int x,int y,int w,int h, Color c,Form1 f,int spd)
        public Rectangle(int x,int y,int w,int h, Color c,Form1 f,int spd) { xVal = x; yVal = y; color = c; width = w; height = h;    frm1 = f; speed = spd;}
        { xVal = x; yVal = y; color = c; width = w; height = h;    frm1 = f; speed = spd;} 
                 public override void  paint(Graphics g)
        public override void  paint(Graphics g) {
        {     try
            try {
            { Thread.Sleep(speed);
                Thread.Sleep(speed); g.DrawRectangle(new System.Drawing.Pen(frm1.BackColor),xVal,yVal,width,height);
                g.DrawRectangle(new System.Drawing.Pen(frm1.BackColor),xVal,yVal,width,height); lock(typeof(Thread))
                lock(typeof(Thread)) {
                { xVal = xVal+ base.directionX;
                        xVal = xVal+ base.directionX;  yVal = yVal+ base.directionY;
                    yVal = yVal+ base.directionY; base.CheckCoordinates();
                    base.CheckCoordinates(); }
                } g.DrawRectangle(new System.Drawing.Pen(color),xVal,yVal,width,height);
                g.DrawRectangle(new System.Drawing.Pen(color),xVal,yVal,width,height);  }
            } catch
            catch {}
            {} 
             }
        } }
    } }
} 
        


和

 using System;
using System; using System.Drawing;
using System.Drawing; using System.Threading;
using System.Threading;


 namespace ThreadTester
namespace ThreadTester {
{ public class Circle : Shapes
    public class Circle : Shapes {
    { public Circle(int x,int y,int w,int h, Color c, Form1 f, int spd)
        public Circle(int x,int y,int w,int h, Color c, Form1 f, int spd) { xVal = x; yVal = y; color = c; width = w; height = h;    frm1 = f; speed = spd;}
        { xVal = x; yVal = y; color = c; width = w; height = h;    frm1 = f; speed = spd;}
 
                         public override void  paint(Graphics g)
        public override void  paint(Graphics g) {
        {     try
            try {
            { Thread.Sleep(speed);
                Thread.Sleep(speed); g.DrawEllipse(new System.Drawing.Pen(frm1.BackColor),xVal,yVal,width,height);
                g.DrawEllipse(new System.Drawing.Pen(frm1.BackColor),xVal,yVal,width,height);  
                         lock(typeof(Thread))
                lock(typeof(Thread)) {
                { xVal = xVal+ base.directionX;
                    xVal = xVal+ base.directionX;  yVal = yVal+ base.directionY;
                    yVal = yVal+ base.directionY; base.CheckCoordinates();
                    base.CheckCoordinates(); }
                } g.DrawEllipse(new System.Drawing.Pen(color),xVal,yVal,width,height);
                g.DrawEllipse(new System.Drawing.Pen(color),xVal,yVal,width,height);  }
            } catch
            catch {    }
            {    }         }
        } }
    } }
}


主界面如下:
全局变量定义如下:
 public volatile Panel panel1;
public volatile Panel panel1; public static Color shapeColor = Color.Blue;
        public static Color shapeColor = Color.Blue; public ColorDialog c;
        public ColorDialog c; public static int threadCount = 0;
        public static int threadCount = 0; private Hashtable threadHolder = new Hashtable();
        private Hashtable threadHolder = new Hashtable(); private const int shapSize = 15;
        private const int shapSize = 15;  private volatile Graphics g;
        private volatile Graphics g; 
        注意其中的:
 private volatile Graphics g;
        private volatile Graphics g; public volatile Panel panel1;
        public volatile Panel panel1;退出部分代码如下:
 private void cmdExit_Click(object sender, System.EventArgs e)
    private void cmdExit_Click(object sender, System.EventArgs e) {
        { /* Abort all the threads that are currently alive */
            /* Abort all the threads that are currently alive */ foreach(Thread t in threadHolder.Values)
            foreach(Thread t in threadHolder.Values) {
            { if(t != null && t.IsAlive)
                    if(t != null && t.IsAlive) t.Abort();
                   t.Abort(); }
            } Form1.ActiveForm.Close();
            Form1.ActiveForm.Close();     }
        } private void cmdColor_Click(object sender, System.EventArgs e)
    private void cmdColor_Click(object sender, System.EventArgs e) {
        { c = new ColorDialog();
            c = new ColorDialog(); c.ShowDialog();
            c.ShowDialog(); shapeColor = c.Color;
            shapeColor = c.Color; }
        }
开始按钮代码 如下:
 private void cmdStart_Click(object sender, System.EventArgs e)
    private void cmdStart_Click(object sender, System.EventArgs e) {
        { Thread t = new Thread(new ThreadStart(StartThread));
            Thread t = new Thread(new ThreadStart(StartThread)); threadHolder.Add(threadCount++,t);
            threadHolder.Add(threadCount++,t); /* Assign a name for each thread (but it's not necessary) */
            /* Assign a name for each thread (but it's not necessary) */ t.Name = "Thread ID: "+threadCount;
            t.Name = "Thread ID: "+threadCount; t.IsBackground  = true;
            t.IsBackground  = true; t.Start();
            t.Start(); }
        } 
        当中对线程的定义部分如下:
 private void StartThread()
    private void StartThread() {
        { /* Create the shape object */
            /* Create the shape object */ Shapes shapes;
            Shapes shapes; if(comboBox1.Text.Equals("Rectangle"))
            if(comboBox1.Text.Equals("Rectangle")) shapes =  new Rectangle(0,0,shapSize,shapSize,shapeColor,this,Convert.ToInt32(comboBoxSpeed.Text.Trim()));
                shapes =  new Rectangle(0,0,shapSize,shapSize,shapeColor,this,Convert.ToInt32(comboBoxSpeed.Text.Trim())); else if(comboBox1.Text.Equals("Circle"))
            else if(comboBox1.Text.Equals("Circle")) shapes =  new Circle(0,0,shapSize,shapSize,shapeColor,this,Convert.ToInt32(comboBoxSpeed.Text.Trim()));
                shapes =  new Circle(0,0,shapSize,shapSize,shapeColor,this,Convert.ToInt32(comboBoxSpeed.Text.Trim()));  /**The line below is not necessary , but if you want to add more shapes this will help*/
                /**The line below is not necessary , but if you want to add more shapes this will help*/ else
            else shapes =  new Rectangle(0,0,shapSize,shapSize,shapeColor,this,Convert.ToInt32(comboBoxSpeed.Text.Trim()));
                shapes =  new Rectangle(0,0,shapSize,shapSize,shapeColor,this,Convert.ToInt32(comboBoxSpeed.Text.Trim())); 
         while(true)
            while(true) {
            { try
                    try { shapes.paint(g);     }
                { shapes.paint(g);     } 
                     catch(Exception e1)
                catch(Exception e1) {
                { Console.WriteLine("Exception in Form1 whileloop >> "+e1);
                        Console.WriteLine("Exception in Form1 whileloop >> "+e1); break;
                    break;      }
                } }
            } }
        } 
                    
                

 
     
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号