C# winform 使用匿名委托使有后台线程显示系统当前时间

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace backthread
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Thread p = new Thread(() =>
              {
                  while (true)
                  {
                      this.Invoke(
                          (MethodInvoker)delegate ()

                         {
                             this.Refresh();
                             Graphics pg = CreateGraphics();
                             pg.DrawString("系统时间:" + DateTime.Now.ToString("yyyy年MM月dd日 HH时mm分ss秒"),
                                 new Font("宋体", 15),
                                 Brushes.Blue,
                                 new Point(10, 10));
                         });
                      Thread.Sleep(1000);
                  }
              });
            p.IsBackground = true;
            p.Start();
        }

    }
}

  运行效果如下图所示:

posted @ 2020-04-16 09:09  星空沃土  阅读(62)  评论(0编辑  收藏  举报