winfrom 怎么在线程中修改控件
1个回答
展开全部
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;
}
}
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;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询