指示灯控件

 

 

 

复制代码
using System;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 指示灯
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        Task task;
        CancellationTokenSource tokenSource = new CancellationTokenSource();

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Visible = true;
            label1.Text = "";//如果觉得太小调label1字体大小
            
            if (task == null || task.IsCompleted)
            {
                task = new Task(new Action(run),tokenSource.Token);
                task.Start();
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            tokenSource.Cancel();
            label1.Visible = false;
        }

        private void run()
        {
            while (true)
            {
                if (label1.ForeColor == Color.Red)
                {
                    label1.ForeColor = Color.Green;
                }
                else
                {
                    label1.ForeColor = Color.Red;
                }
                Thread.Sleep(300);
            }
        }
    }
}
复制代码

 

posted on   hanzq_go  阅读(94)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示