private void Test_Click(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart(ThreadFunction));
th.IsBackground = true;
th.Start();
} private void ThreadFunction()
{
if (RichTextBox1.InvokeRequired)//
{
TestDelegate md = new TestDelegate(ThreadFunction);
this.BeginInvoke(md);
}
else
{
this.test();
th.Abort();
}
}
这是程序代码,执行的test方法是个循环方法,当我点击Test_Click按钮时,界面卡死了,直到这个线程执行完,界面才恢复响应,我大概知道问题出在哪里,好像是我开启的那个线程没起作用,因为RichTextBox1.InvokeRequired始终是false,直接执行else了,请各位大侠帮帮忙,帮我解决一下啊!!!
{
Thread th = new Thread(new ThreadStart(ThreadFunction));
th.IsBackground = true;
th.Start();
} private void ThreadFunction()
{
if (RichTextBox1.InvokeRequired)//
{
TestDelegate md = new TestDelegate(ThreadFunction);
this.BeginInvoke(md);
}
else
{
this.test();
th.Abort();
}
}
这是程序代码,执行的test方法是个循环方法,当我点击Test_Click按钮时,界面卡死了,直到这个线程执行完,界面才恢复响应,我大概知道问题出在哪里,好像是我开启的那个线程没起作用,因为RichTextBox1.InvokeRequired始终是false,直接执行else了,请各位大侠帮帮忙,帮我解决一下啊!!!
这里就会同步线程和界面了。这样没有异步效果。
你没有释放掉对ui的占用。
this.BeginInvoke(md);
改为:
TestDelegate md = new TestDelegate(this.test);
this.Invoke(md);
试试,如果还是有问题,请贴一下你的test的代码,;