跨线程的得到系统时间

来源:互联网 发布:seo在线培训哪家好 编辑:程序博客网 时间:2024/05/23 14:17
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Threading;namespace Clock{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        public void DelegateFunc(string time)        {            //if (this.InvokeRequired)                            //判断当前运行的线程是不是与窗体主线程是同一个线程。            //{            //    this.Invoke(new Action<string>(DelegateFunc), time);            //}            //else            //{            //    textBox_Clock.Text = DateTime.Now.ToString();            //}            this.Invoke((Action)(() =>                {                    textBox_Clock.Text = DateTime.Now.ToString();                }));        }        public void ThreadFunc()                            //定义线程函数        {            while (true)            {                DelegateFunc(DateTime.Now.ToString());                  //用代理的形式实现功能函数            }        }        private void btn_Start_Click(object sender, EventArgs e)        {            //Thread thread = new Thread(new ThreadStart(ThreadFunc));            //thread.IsBackground = true;            //thread.Start();            Action act = new Action(ThreadFunc);            act.BeginInvoke(null, null);            //WaitCallback wcb = new WaitCallback(ThreadFunc);            //ThreadPool.QueueUserWorkItem(wcb, null);        }        private void btn_Stop_Click(object sender, EventArgs e)        {            MessageBox.Show("我不知道该怎么结束");        }    }}
0 0
原创粉丝点击