private void button2_Click(object sender, EventArgs e)
{
    th = new Thread(fun);
    th.Start();
}
private void button3_Click(object sender, EventArgs e)
{
    if(th.IsAlive) {
        label1.Text = "yes";
    }
    else
    {
        label1.Text = "no";
    }
}
fun是空的..我總是得到"no"..我thread開始它了..也沒有中斷它..為何我的thread總不是活著呢?
當button2_Click被點擊時。如果在點擊button3_Click之前該Thread執行完畢,則Thread.IsAlive 回傳 false。
另外,如果Thread已被垃圾收集。在這種情況下,Thread.IsAlive也會回傳false。
using System;
using System.Threading;
using System.Windows.Forms;
namespace ThreadExample
{
    public partial class Form1 : Form
    {
        private Thread th;
        public Form1()
        {
            InitializeComponent();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (!th.IsAlive)
            {
                th = new Thread(Fun);
                th.Start();
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if(th.IsAlive) {
                label1.Text = "yes";
            }
            else
            {
                label1.Text = "no";
            }
        }
        private void Fun()
        {
            // This method will be executed by the new thread
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Thread running: " + i);
                Thread.Sleep(1000);
            }
        }
    }
}