Skip to main content
 首页 » 编程设计

c#之在将 Thread.Sleep 与 pictureBox 一起使用时,Winform 卡住

2024年02月13日38mfryf

我正在为一个项目工作,其中 pictureBox 用于一张一张地显示目录中的图像。

首先我点击开始按钮。它调用一个持续运行的 backgroundWorker,直到我按下停止按钮。然后在 backgroundWorker 中我调用了一个 500ms 时间间隔的计时器。它调用事件处理程序方法。

在 evenhandler 方法中,我首先从数字目录中获取文件名,并在名为“PicBox”的图片框中显示并递增,然后显示下一张图片。我使用了一个 while 循环来运行整个过程,直到我点击停止按钮。

问题是它在 500 毫秒(定时器间隔)后开始并高速显示所有图片。我想在一个流程中展示它们,这样用户就不会意识到所有都是几张图片的组合,但不是以这种速度。

我使用了 Thread.Sleep() 但它卡住了 winform,我什至无法点击停止按钮。

PictureBox代码在这里:

if (_performReceiving) 
{ 
    try 
    { 
        while (_performReceiving) 
        { 
            switch (firstNum) 
            { 
                case 0: 
                case 1: 
                case 2: 
                case 3: 
                case 4: 
                case 5: 
                case 6: 
                case 7: 
                case 8: 
                case 9: 
                    PicBox.Image = Image.FromFile(path + "0" + firstNum + ".png"); 
                    this.PicBox.SizeMode = PictureBoxSizeMode.Zoom; 
                    PicBox.Refresh(); 
                    firstNum++; 
                    break; 
                default: 
                    PicBox.Image = Image.FromFile(path + firstNum + ".png"); 
                    this.PicBox.SizeMode = PictureBoxSizeMode.Zoom; 
                    PicBox.Refresh(); 
                    firstNum++; 
                    break; 
            }  
        } 
    } 
    catch (FileNotFoundException) 
    { 
        MoveTimer.Stop(); 
    } 
} 

有什么建议吗?

编辑:(代码的可视化 View )

Form class 
{ 
start_button clicked() 
{ 
   //call backgroundWorker1 
} 
 
backgroundWorker1_DoWork() 
{ 
   //create Timer1_handler 
   //Timer1.Start() 
} 
 
Timer1_handler() 
{ 
   //Process to get firstNames numeric value which passes to switch parameter 
 
   :: This code posted on the main question :: 
} 
} 

请您参考如下方法:

没有。不不不不不

这都是错的。 Threading 和 WinForm,这些东西你肯定摸不着头脑。 WinForm 在所谓的 UI 线程上运行。这是您必须在其上创建控件的线程,并且是唯一可以访问控件的线程(否则您可能会遇到死锁......或者现在的异常)。

其次,background worker在doWork中使用了一个后台线程。这不是 UI 线程,可以用来做很多可爱的处理。当您使用后台 worker (我忘记了确切的 API)进行更新时,它会自动为您编码到 UI 线程。我们好吗?

好的。现在这是您的问题:System.Windows.Forms.Timer 在 UI THREAD 上运行。这是一个简单的系统,它以特定的时间间隔将项目插入到 UI 线程的队列中。

所以在您的示例中,您似乎正在调用一个后台线程,该线程然后试图将其工作基于位于 UI 线程中的计时器。这太疯狂了。

改为使用 System.Threading.Timer,或者完全丢弃 BackgroundWorker,只用一个线程(UI 线程)来执行此操作。由于您一次只加载 1 张图像,只要图像是本地镜像,您就可以在不使 UI react 迟钝的情况下摆脱它。

Form1_Load(object sender, EventArgs e) { 
    Timer t = new System.Windows.Forms.Timer(); 
    t.Interval = 5000; 
    t.Tick += SomeEventHandlerThatLoadsAndDisplaysTheNextImage; 
    t.Start(); 
} 

或者只是正确使用 BackgroundWorker。如果这样做,您可以添加 .sleeps。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
 
namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
        // this is the UI thread 
        public Form1() 
        { 
            InitializeComponent(); 
            Load += new EventHandler(Form1_Load); 
        } 
 
        private BackgroundWorker worker; 
 
        // this is the UI thread 
        void Form1_Load(object sender, EventArgs e) 
        { 
            worker = new BackgroundWorker(); 
            worker.DoWork += new DoWorkEventHandler(worker_DoWork); 
            worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged); 
            worker.RunWorkerAsync(); 
        } 
        // this is the UI thread, the BackgroundWorker did the marshalling for you (Control.Invoke or Control.BeginInvoke)     
        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) 
        { 
            pictureBox1.Image = (Image) e.UserState; 
        } 
        // this is a background thread, don't touch the controls on this thread we use .ReportProgress (offered by the backgroundWorker) to marshal back to the UI thread. 
        void worker_DoWork(object sender, DoWorkEventArgs e) 
        { 
            while (iNeedToKeepRotatingImages) 
            { 
                Thread.Sleep(5000); 
                var image = LoadAnImage(myState); 
                worker.ReportProgress(0, image); 
            } 
        } 
    } 
    }