把线程做成无限循环
while(true)
{...}或
while(flag)
{...}
可以通过其他线程改变flag而结束这个线程。

解决方案 »

  1.   

    新建一个windows应用程序项目,copy并覆盖form1的代码,即可测试一下using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Threading;namespace TestThread
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button btnThread1;
    private System.Windows.Forms.Button btnStop2;
    private System.Windows.Forms.Button btnThread2;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    /// 
    private bool flag=true;
    private Thread t1,t2; private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.btnThread1 = new System.Windows.Forms.Button();
    this.btnStop2 = new System.Windows.Forms.Button();
    this.btnThread2 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // btnThread1
    // 
    this.btnThread1.Location = new System.Drawing.Point(40, 96);
    this.btnThread1.Name = "btnThread1";
    this.btnThread1.Size = new System.Drawing.Size(88, 32);
    this.btnThread1.TabIndex = 0;
    this.btnThread1.Text = "Thread1";
    this.btnThread1.Click += new System.EventHandler(this.btnThread1_Click);
    // 
    // btnStop2
    // 
    this.btnStop2.Location = new System.Drawing.Point(312, 96);
    this.btnStop2.Name = "btnStop2";
    this.btnStop2.Size = new System.Drawing.Size(72, 32);
    this.btnStop2.TabIndex = 1;
    this.btnStop2.Text = "Stop 2";
    this.btnStop2.Click += new System.EventHandler(this.btnStop2_Click);
    // 
    // btnThread2
    // 
    this.btnThread2.Location = new System.Drawing.Point(168, 96);
    this.btnThread2.Name = "btnThread2";
    this.btnThread2.Size = new System.Drawing.Size(88, 32);
    this.btnThread2.TabIndex = 2;
    this.btnThread2.Text = "Thread2";
    this.btnThread2.Click += new System.EventHandler(this.btnThread2_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(416, 221);
    this.Controls.Add(this.btnThread2);
    this.Controls.Add(this.btnStop2);
    this.Controls.Add(this.btnThread1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main()
    {     
    Application.Run(new Form1());
    } private void btnThread1_Click(object sender, System.EventArgs e)
    {
    t1=new Thread(new ThreadStart(test1));
    t1.IsBackground=true;
    t1.Start();
    } private void test1()
    {
    while(true)
    {
    ///
    ///你要做的事情
    ///

    Console.Write("A");
    }
    } private void test2()
    {
    while(flag)
    {
    ///
    ///你要做的事情
    ///

    Console.Write("B");
    }
    MessageBox.Show("Thread2 Exit");
    } private void btnThread2_Click(object sender, System.EventArgs e)
    {
    t2=new Thread(new ThreadStart(test2));
    t2.IsBackground=true;
    t2.Start();
    } private void btnStop2_Click(object sender, System.EventArgs e)
    {
    flag=false;
    }
    }
    }
      

  2.   

    看看这里有没有你你需要的,
    http://blog.csdn.net/zhzuo/archive/2004/06/10/22037.aspx
    http://blog.csdn.net/zhzuo/archive/2004/06/15/22038.aspx
      

  3.   

    using System;
    using System.Threading;namespace ZZ
    {
    public class ThreadBase
    {
    private Thread thread;
    protected int intInterval = 1000;
    public ThreadBase()
    {
    this.thread = new Thread(new ThreadStart(Run));
    }
    /// <summary>
    /// 线程执行方法
    /// </summary>
    protected virtual void Run()
    {
    while(true) 
    {
    Thread.Sleep(this.intInterval);
    }
    }
    /// <summary>
    /// 启动线程,返回运行结果,若启动线程成功,返回“true”,否则返回“false”
    /// </summary>
    /// <returns></returns>
    public bool Start() 
    {
    bool blnRlt = false;
    if(thread.ThreadState==ThreadState.Stopped)
    {
    this.thread = new Thread(new ThreadStart(Run));

    if(this.thread.ThreadState==ThreadState.Unstarted) 
    {
    try 
    {
    this.thread.Start();
    blnRlt = true;
    }
    catch 
    {
    }
    }
    //else
    // EntS.Resume();
    return blnRlt;
    }
    /// <summary>
    /// 挂起线程
    /// </summary>
    /// <returns></returns>
    public bool Suspend() 
    {
    bool blnRlt = false;
    try 
    {
    this.thread.Suspend();
    blnRlt = true;
    }
    catch 
    {
    }
    return blnRlt;
    }
    /// <summary>
    /// 唤醒线程,返回运行结果,若唤醒线程成功,返回“true”,否则返回“false”
    /// </summary>
    /// <returns></returns>
    public bool Resume() 
    {
    bool blnRlt = false;
    try 
    {
    this.thread.Resume();
    blnRlt = true;
    }
    catch 
    {
    }
    return blnRlt;
    }
    /// <summary>
    /// 终止线程,停止参数转换服务
    /// </summary>
    /// <returns></returns>
    public bool Abort() 
    {
    bool blnRlt = false;
    if ((this.thread.ThreadState & (ThreadState.Unstarted | ThreadState.Stopped | ThreadState.SuspendRequested | ThreadState.Suspended))==0)
    try 
    {
    this.thread.Abort();
    blnRlt = true;
    }
    catch 
    {
    }
    return blnRlt;
    }
    /// <summary>
    /// 获取线程状态
    /// </summary>
    public ThreadState State
    {
    get{return this.thread.ThreadState;}
    }
    }
    }