[英]In C# winforms, why is the UI label not updating and keeping up with backgroundworker progresschanged event?
I am updating a label text on a backgroundworker progressChanged event (literally one 1 line of code in this event callback). 我正在更新backgroundworker progressChanged事件上的标签文本(在此事件回调中实际上是一行代码)。
Its updating about 50 times in around 45 seconds and the issue is that the UI only seems to update about 10 times. 它在大约45秒内更新约50次,问题是UI似乎只更新了大约10次。 So it appears that the UI is not "keeping up" with the number of events.
因此,UI似乎没有“跟上”事件的数量。
Is there anyway to help improve this. 无论如何都有助于改善这一点。 I see folks suggestions APplication.DoEvents() but always thought that was a bit dodgy.
我看到人们建议APplication.DoEvents()但总是认为有点狡猾。
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
lblProgress.Text = "Updating Progress: " + e.ProgressPercentage;
}
Is there a recommended practice here so i see every update on the UI? 这里有推荐的做法,所以我看到UI上的每个更新?
Try to call: 试着打电话:
this.lblProgress.Refresh();
after setting the Text-Property 设置Text-Property后
I have only had this happen once before, and at the time, the best method I found was to just call update: 我之前只发生过这种情况,当时我发现的最好的方法就是调用更新:
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
lblProgress.Text = "Updating Progress: " + e.ProgressPercentage;
lblProgress.Update();
}
Depending on how important your label's text is, you could call Refresh()
instead for a more immediate redraw. 根据标签文本的重要程度,您可以调用
Refresh()
代替更直接的重绘。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.