我似乎有正确的顺序执行命令的问题,我在我的程序的方法:C#方法,说明以正确的顺序不执行
private void GenerateButton_Click(object sender, EventArgs e)
{
Statuslabel.Text = "Working...";
LongMethod();
//Call to another Method of another class which takes 15-20 seconds to execute
Statuslabel.Text = "Done";
}
的问题似乎是,而不是分配“工作“状态标签,然后调用LongMethod
,程序似乎首先执行LongMethod()
,然后它将状态标签的文本更改为”工作“一秒钟,然后立即将其更改为”完成“。 哦,并且在执行LongMethod()
期间UI被锁定,因为程序是单线程的。
我试过线程更早,但对我的生活我不能得到正确的语法,我想:
Thread MyThread = new Thread(LongClass.LongFunction);
Thread MyThread = new Thread(new ThreadStart(LongClass.LongFunction));
哪里LongClass
是包含LongFunction
作为一个静态方法的类。 我会检查出现在的背景工作者。
如果您在'LongMethod()'之前向'Application.DoEvents()'添加了一个调用,那么您将使用临时创可解决此问题。杰森的回答是正确的方法。 –