public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart(infoss));
th.IsBackground = true;
th.Start();
}
private void infoss()
{
while (true)
{
SetTime();
Thread.Sleep(1000);
}
}
public delegate void SetTimeInfo(string timeinfo);//委托
private void SetTime()
{
SetTimeInfo info = new SetTimeInfo(SetTextTime);
this.BeginInvoke(info, DateTime.Now.ToString());//异步执行
}
private void SetTextTime(string timeinfo)
{
if (this.textBox1.InvokeRequired)
{
SetTimeInfo time = new SetTimeInfo(SetTextTime);
this.textBox1.BeginInvoke(time, timeinfo);
}
else
{
this.textBox1.Text = timeinfo;
}
}