【发布时间】:2015-01-04 12:51:58
【问题描述】:
这是冻结当前窗口的代码。如何使这个表格不冻结。
public partial class Form1 : Form
{
Thread t;
int s = 0;
public Form1()
{
InitializeComponent();
label2.Text = "Push the Button";
button1.Text = "Push me!";
button1.Click += new EventHandler(button1_Click);
this.Controls.Add(label2);
this.Controls.Add(button1);
}
void button1_Click(object sender, EventArgs e)
{
t = new Thread(new ThreadStart(RunMe));
t.Start();
}
private void RunMe()
{
if (!InvokeRequired)
{
while(true)
{
label2.Text = s.ToString();
s++;
Task.Delay(10000).Wait(10000);
}
}
else
{
Invoke(new ThreadStart(RunMe));
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
【问题讨论】:
-
Invoke在 UI 线程上运行一些代码,因此您实际上是在 UI 线程上运行整个无限循环,导致它被永久阻塞。跨度>
标签: c# multithreading asynchronous invoke