C#用委托来动态显示日期和时间

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 _01测试委托与事件
{

    public delegate void DeShowTime();

 
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Thread thread = new Thread(ShowTime);
            thread.IsBackground = true;
            thread.Start();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        public void ShowTime()
        { 
            while (true)
            {
                //Lambda表达式
                //DeShowTime dst = () =>
                //{
                //    textBox1.Text = "当前时间:" + DateTime.Now.ToString();
                //};

                //匿名函数委托
                DeShowTime dst = delegate
                {
                    textBox1.Text =  DateTime.Now.ToString();
                    toolStripStatusLabel1.Text = "当前时间: " + DateTime.Now.ToString();
                };

                dst();
                Thread.Sleep(1000);
            }
        }

    }
}

  

 

posted @ 2020-12-18 10:43  Nanyingming  阅读(337)  评论(0编辑  收藏  举报