简体   繁体   English

从后台线程更新时,UI会略微冻结

[英]UI slightly freezes when update from background thread

I have a Winform with 4 PictureBox controls, each control will contain a diferent image. 我有一个带有4个PictureBox控件的Winform,每个控件将包含一个不同的图像。 The process is: 过程是:

An event x is raised, the eventargs from this event, contain the filenames of each image (4), an so on (file exists etc..). 引发事件x,来自此事件的eventargs包含每个图像的文件名(4),依此类推(文件存在等等)。 Then, I have to update the UI. 然后,我必须更新UI。

Commonly I use Invoke: 通常我使用Invoke:

Invoke((ThreadStart)delegate()
{
    picBig.Image = new Bitmap(strImageBig);
    picLittle1.Image = new Bitmap(saLittle[0]);
    picLittle2.Image = new Bitmap(saLittle[1]);
    picLittle3.Image = new Bitmap(saLittle[2]);
});

// saLittle[] is a string array, contains, filenames: "image1.jpg"

But when this executes, the form freezes for a little time, about 500ms, I know it's a small interval but it's noticeable, then the app continues normally. 但是当这个执行时,表单会冻结一段时间,大约500毫秒,我知道这是一个很小的间隔,但它很明显,然后应用程序继续正常。

I was trying to find out the reason of that 'UI freeze', then, after research, I found BeginInvoke. 我试图找出'UI冻结'的原因,然后,经过研究,我找到了BeginInvoke。 Now my code looks like this: 现在我的代码看起来像这样:

BeginInvoke((MethodInvoker)delegate
{
    picBig.Image = new Bitmap(strImageBig);
    picLittle1.Image = new Bitmap(saLittle[0]);
    picLittle2.Image = new Bitmap(saLittle[1]);
    picLittle3.Image = new Bitmap(saLittle[2]);
});

This is a little faster. 这快一点。 But the UI is still freezing for 200~300ms. 但UI仍然会冻结200~300ms。

In the articles I've read, they say that BeginInvoke is a better way than Invoke. 在我读过的文章中,他们说BeginInvoke比Invoke更好。

The code is working OK, there is no problem with logic or anything else. 代码工作正常,逻辑或其他任何问题都没有问题。 I just want to know why this happens. 我只想知道为什么会这样。 I don't want to leave this doubt unclear. 我不想不明白这个疑问。 The project is already finished. 该项目已经完成。 Hope this be useful for someone else. 希望这对其他人有用。

Maybe this is not the correct approach. 也许这不是正确的方法。 I know there are many ways to update the UI from a background thread, but is there another way to make an update faster ? 我知道有很多方法可以从后台线程更新UI,但还有另一种方法可以更快地进行更新吗?

Or, do you think, the image loading is the reason? 或者,你认为,图像加载是什么原因? Is there another way to do a faster loading of images? 还有另一种方法可以更快地加载图像吗?

Thanks in advance. 提前致谢。

This is because you're actually loading your images from disk on the UI thread along with setting the contents of the control. 这是因为您实际上是在UI线程上从磁盘加载图像以及设置控件的内容。 Calling the Bitmap constructor with a file path will go to the hard drive and load the image into memory. 使用文件路径调用Bitmap构造函数将转到硬盘驱动器并将映像加载到内存中。

Invoke and BeginInvoke will run the delegate that you provide on the thread that the control was created on, which is most likely going to be the UI thread. InvokeBeginInvoke将运行您在创建控件的线程上提供的委托这很可能是UI线程。

...but is there another way to make an update faster? ...但还有另一种方法可以更快地进行更新吗?

Load the images on your background thread and, when they're actually loaded, invoke and set the images into the controls. 在后台线程上加载图像,当它们实际加载时,调用并将图像设置到控件中。

                    var big = new Bitmap(strImageBig);
                    var little1 = new Bitmap(saLittle[0]);
                    var little2 = new Bitmap(saLittle[1]);
                    var little3 = new Bitmap(saLittle[2]);

                    Invoke((ThreadStart)delegate()
                    {
                        picBig.Image = big;
                        picLittle1.Image = little1;
                        picLittle2.Image = little2;
                        picLittle3.Image = little3;
                    });                    

But when this executes, the form freezes for a little time, about 500ms, I know it's a small interval but it's noticeable, then the app continues normally. 但是当这个执行时,表单会冻结一段时间,大约500毫秒,我知道这是一个很小的间隔,但它很明显,然后应用程序继续正常。

Eventually, the UI thread needs to actually update the images. 最终,UI线程需要实际更新图像。 When the images are generated and updated on the UI thread, this will cause a (short) delay. 在UI线程上生成和更新图像时,这将导致(短暂)延迟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM