当我试图计算RSS一切似乎都很好我检查调试,它没关系后,我降级为for循环的值,但即使这样程序没有完成。当ı开始编程WindowsForm没有响应,我搜索解决方案,并找到一个关于打开新线程的代码。但没有任何改变。总之,程序无穷无尽。谢谢你的合作。
private async void button2_Click(object sender, EventArgs e)
{
button1.Enabled = false;
button2.Enabled = false;
var count = 0;
double RSS = 0;
double RSS2 = 0;
int W1 = 0;
int W0 = 0;
int xi = 0;
int a = 0;
await Task.Run(() =>
{
for (int w0 = 100000; w0 <= 150000; w0 = +10000)
{
UIupdate(w0);
count = w0;
for (int w1 = -10; w1 <= 10; w1++)
{
for (a = 0; a < 100; a++)
{
RSS = Math.Tan(w1) * column1[a] + w0;
if (RSS2 == 0)
{
RSS2 = RSS;
W1 = w1;
W0 = w0;
xi = a;
}
if (RSS2 > RSS)
{
RSS2 = RSS;
W1 = w1;
W0 = w0;
xi = a;
}
}
}
}
}
);
button1.Enabled = true;
button2.Enabled = false;
label7.Text = RSS2.ToString();
label8.Text = W0.ToString();
label9.Text = W1.ToString();
label10.Text = column1[xi].ToString();
}
private void UIupdate(int w0)
{
var timenow = DateTime.Now;
if ((DateTime.Now - dt).Milliseconds<=50)
{
synchronizationcontext.Post(new SendOrPostCallback(o => {
label1.Text = "first intercept point: " + (int)o;
return;
}),w0);
dt = timenow;
}
}
首先,尝试添加Application.DoEvents();在你的UIupdate方法中。它应该工作。
假设您调用的方法甚至在任务内部更新UI,它将调用该方法在主线程中运行...这就是您的应用程序冻结的原因。
另一个建议是删除async / await关键字,然后使用创建的任务中的ContinuesWith方法将剩余的代码移动到任务。