C#,纯WinForm打造指示灯

核心代码如下

//开始
private void button1_Click(object sender, EventArgs e)
{
    label1.Visible = true;
    label1.Text = "●";//如果觉得太小调label1字体大小
    if (th == null || !th.IsAlive)
    {
        th = new Thread(run);//添加线程 
        th.IsBackground = true;
        th.Start();
    }
}
//结束
private void button2_Click(object sender, EventArgs e)
{
    if (th != null && th.IsAlive)
    {
        th.Abort();
    }
    label1.Visible = false;
}
Thread th;
private void run()
{
    while (true)
    {
        if (label1.ForeColor == Color.Red)
        {
            label1.ForeColor = Color.Green;
        }
        else
        {
            label1.ForeColor = Color.Red;
        }
        Thread.Sleep(300);
    }
}