加入线程,根据不同的时间转换颜色。
等有时间,写一个交通红路灯,改一改就行。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsLamp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnopen_Click(object sender, EventArgs e)
{
lblamp.Visible = true;
lblamp.Text = "●"+"1";//如果觉得太小调label1字体大小
if (th == null || !th.IsAlive)
{
th = new Thread(run);//添加线程
th.IsBackground = true;
th.Start();
}
}
private void btnclose_Click(object sender, EventArgs e)
{
if (th == null || th.IsAlive)
{
th.Abort();
}
lblamp.Visible = false;
}
Thread th;
private void run()
{
while (true)
{
if (lblamp.ForeColor == Color.Red)
{
lblamp.ForeColor = Color.Green;
}
else
{
lblamp.ForeColor = Color.Red;
}
Thread.Sleep(300);
}
}
}
}