Using BackgroundWorker to update the UI without freezes…?

拥有回忆 提交于 2019-12-03 08:46:11

A) You probably don't need to use this.Invoke and instead use this.BeginInvoke. Invoke blocks the current thread.

B) You don't need to define your own delegates you can use MethodInvoker

if(this.InvokeRequired) {
  this.BeginInvoke(new MethodInvoker(() => PopulateThread(e)));
  return;
}

It's much cleaner :)

You are using Control.Invoke to execute just about everything, meaning this code isn't multithreaded at all.

The proper way (involving a Backgroundworker) would be to use the UpdateProgress event to add elements. It is already synchronized.

But since you're hiding the control (or is it the Form ?) during this process you might as well build a List and on completion add it to the Listview. That piece of code shouldn't take long.

Or some sort of combination, adding small lists in an update event. And I wonder about the wisdom of Hide/Show, I expect this to just make the UI flicker. Leave them out or replace with SuspendLayout/Resumelayout.

Pump the events manually with

Application.DoEvents(); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!