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("我不知道该怎么结束");
}
}
}
跨线程的得到系统时间
最新推荐文章于 2024-05-27 17:20:40 发布