C#,winform-Spinning Wheel进程中断并间歇性恢复

本文关键字:恢复 中断 winform-Spinning Wheel 进程 | 更新日期: 2023-09-27 18:28:04

当用户启动长时间运行的过程时,显示旋转轮进度动画gif。当我点击开始时,过程开始,同时轮子开始旋转。

但问题是,在长期运行过程中,车轮会在两者之间颠簸并重新开始。它应该是连续旋转的。我在同一个线程中运行任务和动画gif(,因为指示器只是一个动画图像,而不是真正的进度值)。

使用的代码是,

        this.progressPictureBox.Visible = true;
        this.Refresh(); // this - an user controll
        this.progressPictureBox.Refresh();
        Application.DoEvents();
        OnStartCalibration(); // Starts long running process
        this.progressPictureBox.Visible = false;

   OnStartCalibration()
   {      
        int count = 6;  
        int sleepInterval = 5000;
        bool success = false;
        for (int i = 0; i < count; i++)
        {
            Application.DoEvents();
            m_keywordList.Clear();
            m_keywordList.Add("HeatCoolModeStatus");
            m_role.ReadValueForKeys(m_keywordList, null, null);
            l_currentValue = (int)m_role.GetValue("HeatCoolModeStatus");
            if (l_currentValue == 16)
            {
                success = true;
                break;
            }    
            System.Threading.Thread.Sleep(sleepInterval);
        }
}

如何显示车轮的不间断连续显示,直到过程结束?

C#,winform-Spinning Wheel进程中断并间歇性恢复

如果使用框架4,请将OnStartCalibration(); // Starts long running process行替换为以下代码:

BackgroundWorker bgwLoading = new BackgroundWorker();
bgwLoading.DoWork += (sndr, evnt) =>
{
    int count = 6;  
    int sleepInterval = 5000;
    bool success = false;
    for (int i = 0; i < count; i++)
    {
        Application.DoEvents();
        m_keywordList.Clear();
        m_keywordList.Add("HeatCoolModeStatus");
        m_role.ReadValueForKeys(m_keywordList, null, null);
        l_currentValue = (int)m_role.GetValue("HeatCoolModeStatus");
        if (l_currentValue == 16)
        {
            success = true;
            break;
        }    
        System.Threading.Thread.Sleep(sleepInterval);
    }
};
bgwLoading.RunWorkerAsync();

您不能在同一个线程上运行进度指示和任务。你应该使用BackgroundWorker

您的GUI线程将订阅ProgressChanged事件,并将收到任务更新的通知。从这里,您可以适当地更新进度指示。还有任务完成时的事件。