如题。

解决方案 »

  1.   

    在线程对象上调用Abort()即可在线程中引发异常,然后在线程中捕获到该异常后做退出线程的处理。
      

  2.   

    using System;
    using System.Threading;
    using System.Collections.Generic;
    using System.Windows.Forms;public class MyClass
    {
    public static void Main()
    {
    Application.Run(new FormMain());
    }

    public class FormMain :Form
    {
    Thread thread1;
    Thread thread2;
    Button buttonCloseThread1;

    private void Thread1Method()
    {
    while(true)
    {
    Console.WriteLine("Thread 1 at:" + DateTime.Now);
    Thread.Sleep(2000);
    }
    }

    private void Thread2Method()
    {
    while(true)
    {
    Console.WriteLine("Thread 2 at:" + DateTime.Now);
    Thread.Sleep(2000);
    }
    }

    public FormMain()
    {

    thread1 = new Thread(new ThreadStart(Thread1Method));
    thread2 = new Thread(new ThreadStart(Thread2Method));
    thread1.Start();
    thread2.Start();


    this.buttonCloseThread1 = new Button();
    this.buttonCloseThread1.Text = "Closed Thread 1";
    this.Controls.Add(this.buttonCloseThread1);

    this.buttonCloseThread1.Click += delegate{
    if( thread1 != null )
    thread1.Abort();
    };
    }
    }
    }
      

  3.   


    using System;
    using System.Threading;
    using System.Collections.Generic;
    using System.Windows.Forms;public class MyClass
    {
    public static void Main()
    {
    Application.Run(new FormMain());
    }

    public class FormMain :Form
    {
    Thread thread1;
    Thread thread2;
    Button buttonCloseThread1;

    private void Thread1Method()
    {
    while(true)
    {
    Console.WriteLine("Thread 1 at:" + DateTime.Now);
    Thread.Sleep(2000);
    }
    }

    private void Thread2Method()
    {
    while(true)
    {
    Console.WriteLine("Thread 2 at:" + DateTime.Now);
    Thread.Sleep(2000);
    }
    }

    public FormMain()
    {

    thread1 = new Thread(new ThreadStart(Thread1Method));
    thread2 = new Thread(new ThreadStart(Thread2Method));
    thread1.Start();
    thread2.Start();


    this.buttonCloseThread1 = new Button();
    this.buttonCloseThread1.Text = "Closed Thread 1";
    this.Controls.Add(this.buttonCloseThread1);

    this.buttonCloseThread1.Click += delegate{
    if( thread1 != null )
    thread1.Abort();
    };
    }
    }
    }
      

  4.   

    1楼太糊弄人了吧!! static volatile bool flag=false ;
            static void run() 
            {
                while (!flag) 
                {
                    Console.WriteLine("run");
                    Thread.Sleep(1000);
                }        }
       Thread t = new Thread(run);
                t.Start();
                int i=0;
                while (i<10)
                {
                    Thread.Sleep(1000);
                    i++;
                }
                flag = true;
                t.Join();
                Console.WriteLine("Complete");
                Console.Read();