prevent从加工过程中暂停一个WinForms图片框动画GIF?过程中、加工、动画、图片

2023-09-03 08:08:59 作者:中国好人家

我一张有2控制的一种形式。控制1有多个搜索条件,用户可以点击一个按钮,开始搜索。控制2有一个GridView将显示在搜索结果中,也有与动画GIF图片框在里面。

I have a form with 2 controls on it. Control 1 has several search criteria and the user can click a button to start the search. Control 2 has a GridView that will display the search results and also has a PictureBox with an animated Gif in it.

1的控制中有一个方法,有一个嵌套的foreach循环需要5分钟以上才能完成(取决于搜索条件,有时它完成几乎瞬间)。

Control 1 has a method in it that has a nested foreach loop that takes upwards of 5 minutes to complete (depends upon the search criteria, sometimes it finishes almost instantly).

什么情况是,我呼吁控制2的方法,使图片框可见,gif动画开始运行。一旦foreach循环被打的需要一段时间,图像完全停止。每当我出现任何提示消息框图像再次开始运行(我presume这是因为在MessageBox阻止)。只要在MessageBox被关闭时,图像停止动画

What happens is that I call a method on Control 2 that makes the PictureBox visible and the animated Gif starts running. As soon as the foreach loop is hit that takes a while, the image completely stops. Whenever I show any MessageBoxes the image starts running again (I presume it is because the MessageBox is blocking). As soon as the MessageBox is closed, the image stops animating.

我presume我将需要利用一些与线程,但一切我想(ImageAnimator,线程调用,调用调用的方法等),似乎都无法正常工作。我需要什么样的事件/库与以使这项工作的工作任何提示?任何好的例子可循?

I presume I will need to utilize something with threading, but everything I am trying (ImageAnimator, Thread calling a method that calls Invoke, etc.) all seem to not work. Any hints what events/libraries I need to work with in order to make this work? Any good examples to follow?

推荐答案

您长时间运行code运行在GUI线程,并阻止内部消息循环,所以任何GUI被停止。尝试在一个单独的工作线程运行长时间运行code。

You long running code runs in the GUI-Thread and blocks the internal message loop, so any GUI is stopping. Try to run your long running code in a separate worker thread.