最近在工作中接触了多线程的项目,对两个线程初步的了解了一些,现在又要做一个三个线程的问题。
请各位高手指点一下。
能举个简单的例子最好,写两三个方法,方法体和方法的功能用文字说明一下就行,涉及到多线程的语句最好写出来。
最好举个三线程的例子!
多谢了!

解决方案 »

  1.   

    Thread _t1 = new Thread(new TreadStart(方法1));
    Thread _t2 = new Thread(new TreadStart(方法2));
    Thread _t3 = new Thread(new TreadStart(方法3));
    _t1.start();
    _t2.start();
    _t3.start();
      

  2.   

    那你在方法里面的最后判断就是了
    比如你的方法中有个循环,那你在循环结束的时候直接用个MessageBox.Show(方法运行结束);如果可以的话加个Application.DoEvent();这样方法在运行的时候,你想干什么就干什么去
      

  3.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    using System.Drawing.Drawing2D;
    using System.Diagnostics;
    namespace Sample080410
    {
        public partial class Form1 : Form
        {
            Bitmap Rectimage = new Bitmap(@"E:\\Rectangle.bmp", true);
            Bitmap Triangleimage = new Bitmap(@"E:\\Triangle.bmp", true);
            Bitmap Circleimage = new Bitmap(@"E:\\Circle.bmp", true);        int i = 1;
            Pen pen = new Pen(Color.Red);
            //以下几个point定义无效,实际运行无效果
            //Point p1 = new Point(0, 100);
            //Point p2 = new Point(700, 100);
            //Point p3 = new Point(300, 0);
            //Point p4 = new Point(300, 700);
            //Point p5 = new Point(100, 0);
            //Point p6 = new Point(100, 700);
            //Point p7 = new Point(500, 0);
            //Point p8 = new Point(500, 700);
            int rotate_Y = 100;
            int move_Y = 0;
            int z = 15;
            int trianglemove_Y = 0;
            int circlemove_Y = 0;        private System.Windows.Forms.Timer timer1;        public Form1()
            {
                InitializeComponent();
                //下面这行代码是把Form的Load事件绑定为Form1_Load,如果不写这行代码,你要手工将设置
                this.Load+=new EventHandler(Form1_Load);
            }        
            private void Form1_Load(object sender, EventArgs e)
            {
                this.timer1 = new System.Windows.Forms.Timer();
                timer1.Tick+=new EventHandler(timer1_Tick);
                this.timer1.Interval = 1000;
                this.timer1.Enabled = true;
            }
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                //在需要重绘窗体的时候,绘制图片,以保持形状,但不移动图片位置
                  AllPicture(false, e.Graphics);
                e.Graphics.Dispose();
            }        private void AllPicture(bool movePic,Graphics g)
            {
                if(g==null)
                    g = this.CreateGraphics();
                //以下四行代码无效因为后面是g.Clear()
                //g.DrawLine(pen, p1, p2);
                //g.DrawLine(pen, p3, p4);
                //g.DrawLine(pen, p5, p6);
                //g.DrawLine(pen, p7, p8);
                g.Clear(Color.White);            PointF point = new PointF(100, rotate_Y + move_Y);
                Matrix Matrix = new Matrix();
                Matrix.RotateAt(z, point);
                g.Transform = Matrix;
                g.DrawImageUnscaled(Rectimage, 50, 50 + move_Y);            if (i % 2 == 0)
                {
                    if (movePic) trianglemove_Y += 1;
                    PointF pointrotate = new PointF(300, rotate_Y + trianglemove_Y);
                    Matrix Matrix2 = new Matrix();
                    Matrix2.RotateAt(z, pointrotate);
                    g.Transform = Matrix2;
                    g.DrawImageUnscaled(Triangleimage, 250, 50 + trianglemove_Y);
                }
                else
                {                PointF pointrotate = new PointF(300, rotate_Y + trianglemove_Y);
                    Matrix Matrix2 = new Matrix();
                    Matrix2.RotateAt(z, pointrotate);
                    g.Transform = Matrix2;                g.DrawImageUnscaled(Triangleimage, 250, 50 + trianglemove_Y);
                }
                if (i % 3 == 0)
                {
                    if (movePic) circlemove_Y += 1;                PointF circlepoint = new PointF(500, rotate_Y + circlemove_Y);
                    Matrix Matrix3 = new Matrix();
                    Matrix3.RotateAt(0, circlepoint);
                    g.Transform = Matrix3;                g.DrawImageUnscaled(Circleimage, 450, 50 + circlemove_Y);
                }
                else
                {
                    PointF circlepoint = new PointF(500, rotate_Y + circlemove_Y);
                    Matrix Matrix3 = new Matrix();
                    Matrix3.RotateAt(0, circlepoint);
                    g.Transform = Matrix3;                g.DrawImageUnscaled(Circleimage, 450, 50 + circlemove_Y);
                }
                if (movePic)
                {
                    move_Y += 1;
                    z += 15;
                }
                g.Dispose();
                if (i < 60)
                    i++;
                else if (i==60)
                    timer1.Enabled = false;//执行60次,不再使用timer
            }
            private void timer1_Tick(object sender, EventArgs e)
            {
                AllPicture(true,null);
            }
        }
    }
      

  4.   

    找网上找一个Socket 通信实例,里面就有详细编写方法,我以前就是这样开始学习的.
      

  5.   

    Thread _t1 = new Thread(new TreadStart(方法1)); 
    这样不好 有数量限制
    用ThreadPool这个
    public class ThreadBase
    {
        public static void Main ( ) {        System.Threading.WaitCallback waitCallback = new WaitCallback ( MyThreadWork );
    System.Threading.WaitCallback waitCallback = new WaitCallback ( MyThreadWork1 );
    System.Threading.WaitCallback waitCallback = new WaitCallback ( MyThreadWork2 );
    System.Threading.WaitCallback waitCallback = new WaitCallback ( MyThreadWork3 );        ThreadPool.QueueUserWorkItem ( waitCallback, "第一个线程" );
            ThreadPool.QueueUserWorkItem ( waitCallback1, "第二个线程" );
            ThreadPool.QueueUserWorkItem ( waitCallback2, "第三个线程" );
            ThreadPool.QueueUserWorkItem ( waitCallback3, "第四个线程" );
            Console.ReadLine ( );
        }    public static void MyThreadWork ( object state ) {
            Console.WriteLine ( "线程现在开始启动…… {0}",(string)state );
            Thread.Sleep ( 10000 );
            Console.WriteLine ( "运行结束…… {0}",( string ) state );
        }
    }
      

  6.   

    使用线程并不一是异步完成任务,有可能CUP全被占用而界面没有响应。如果线程可能是一个无限的操作,比如是一个大的循环,则可以在每一个循环过程中使用Thread.Sleep(num)来使其暂停使得其它线程得到响应,也可以使用Application.DoEvents()方法。
      

  7.   

    使用多线程加载多个Xml文件到TreeView控件
    http://blog.csdn.net/zhzuo/archive/2004/06/10/22037.aspx
    编写多线程网络检测程序
    http://blog.csdn.net/zhzuo/archive/2004/07/08/37262.aspx
      

  8.   

    其实,在.net 2.0后提供了 BackgroundWorker,专门来解决你碰到的问题,使用这个组件就够了,MSDN有很多文档。
      

  9.   

    BackgroundWorker,好主意,回去研究一下
      

  10.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Threading;namespace CloseThreadBack
    {
         delegate void Comp();
        class BackThread///逻辑层执行的多线程调用类,我用的是线程池跟直接用线程类是一样的。
        {
            public event Comp Finish;
            List<Thread> list = new List<Thread>();
            public void ThreadsWork()
            {
                for (int i = 0; i < 3; i++)
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(xx));
                }
            }
            int n = 0;
            private void message()
            {
                n ++;
                if (n == list.Count)//判断最后一个线程是不是执行完成了
                {
                    if (Finish != null)
                    {
                        Finish();
                    }
                }
            }        private void xx(object obj)
            {
                try
                {
                    //Thread.CurrentThread;
                    list.Add(Thread.CurrentThread);
                    DataTable table = new DataTable();
                    table.Columns.Add("id", typeof(int));
                    table.Columns.Add("name", typeof(string));
                    for (int i = 0; i < 1000000; i++)
                    {
                        DataRow row = table.NewRow();
                        row[0] = i;
                        row[1] = "wu" + i.ToString();
                    }
                    message();
                }
                catch
                { }
            }        public void Close()//关闭所有的线程执行
            {
                try
                {
                    foreach (Thread thread in list)
                    {
                        thread.Abort();
                    }
                }
                catch
                { }
            }
        }
    }////////////////UI层,只有两个按钮button1,button2,
    BackThread backthread;
            public Form1()
            {
                InitializeComponent();
                backthread = new BackThread();
                backthread.Finish += new Comp(backthread_Finish);
            }        void backthread_Finish()
            {
                MessageBox.Show("Complete!");
            }
            
            private void button1_Click(object sender, EventArgs e)//运行线程
            {            
                backthread.ThreadsWork();
            }        private void button2_Click(object sender, EventArgs e)//结束线程
            {
                if(backthread != null)
                backthread.Close();
            }
    当你在执行线程任务时也可以做其他的事情,后台所有线程都执行完后会提示你任务完成。