using System.Threading; //线程
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Thread go;
private delegate void aaaaa(); // 定义委托
public Form1()
{
InitializeComponent();
end_button.Enabled = false;
}
private void start_button_Click(object sender, EventArgs e)
{
end_button.Enabled = true;
go = new Thread(new ThreadStart(starttime)); //开启线程
go.Start();
}
public void starttime()
{
while (true)
{
Thread.Sleep(111);
setText();
}
}
private void setText()
{
try
{
if (this.time_textBox.InvokeRequired)
{
aaaaa fd = new aaaaa(setText);
this.Invoke(fd);
}
else
{
time_textBox.Text = System.DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss");
time.Text = time_textBox.Text;
}
}
catch (Exception e)
{
go.Abort();
MessageBox.Show(e.ToString());
}
}
private void end_button_Click(object sender, EventArgs e)
{
go.Abort();
end_button.Enabled = false;
}
}
}
C# 线程
最新推荐文章于 2024-08-23 10:35:13 发布