日期:2014-05-18 浏览次数:21262 次
namespace MyThread
{
    class MyThread_class
    {
        Thread thread = null; //创建一个全局线程变量
        //用来接收传进来的参数Pt1,可以在RuleMethod方法里面用这个全局变量
        string Parameter1;  
        //当然这里可以另N个参数,数据类型随便定,只要传的时候数据类型一样就行
        public MyThread_class(string Pt1)  
        {
            this.Parameter1 = Pt1;
        }
        
        public void start()  //线程开启
        {
            //把你RuleMethod方法放进来
            this.thread = new Thread(RuleMethod);  
            this.thread.Start();
        }
        public void stop()  //线程停止
        {
            this.thread.Abort();
        }
    }
}
//然后在另一个窗体里面调用这个类,例如Button的Click事件里面
Hashtable  Ht_Save_MyThread = new Hashtable();  //新建一个Hashtable全局变量
private void button1_Click(object sender, EventArgs e)
{
    for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++)
    {
        MyThread_class mythread=new  MyThread_class("你要传给线程的参数Pt1");  //实例化一个MyThread_class类的对象
         mythread.start();
        Ht_Save_MyThread.Add(i,mythread);  //存放MyThread_class类
    }
}
//停止线程
private void button2_Click(object sender, EventArgs e)
{
    for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++)
    {
         MyThread_class Take_Thread = (MyThread_class)Ht_Save_MyThread[i]//从Hashtable里面取MyThread_class类
          Take_Thread.stop();
    }
}