简体   繁体   English

在C#winforms中,为什么UI标签没有更新并跟上backgroundworker progresschanged事件?

[英]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.

相关问题 后台工作人员进度更改事件C# - Backgroundworker progresschanged event c# 为什么在backgroundworker progresschanged事件中更新标签时为什么出现跨线程异常? - Why i'm getting cross thread exception while updating label in backgroundworker progresschanged event? c#如何使用BackgroundWorker的ProgressChanged事件在线程中间做UI动作 - c# how to use ProgressChanged event of BackgroundWorker to do UI actions in the middle of thread (C#) BackgroundWorker() ProgressChanged 不工作 - (C#) BackgroundWorker() ProgressChanged not working c# backgroundWorker 未引发 ProgressChanged 或 RunWorkerCompleted 事件 - c# backgroundWorker not raising ProgressChanged or RunWorkerCompleted events 为什么BackgroundWorker不需要在ProgressChanged事件处理程序中调用? - Why doesn't a BackgroundWorker need Invoke in the ProgressChanged event handler? 为什么BackgroundWorker的ProgressChanged事件在不调用RunWorkerAsync的情况下工作? - Why does the BackgroundWorker's ProgressChanged event work without calling RunWorkerAsync? C#BackgroundWorker - 最好将结果保存到文件本身还是使用ProgressChanged? - C# BackgroundWorker - better to save results to a file itself or use ProgressChanged? 在调用ReportProgress之后,不会立即触发C#BackgroundWorker.ProgressChanged - C# BackgroundWorker.ProgressChanged not fired immediately after ReportProgress is called UIProgressView不从BackgroundWorker.ProgressChanged更新 - UIProgressView not updating from BackgroundWorker.ProgressChanged
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM