【问题标题】:C#, winform - Spinning Wheel progress breaks and resumes intermittentlyC#,winform - 纺车进度中断并间歇性恢复
【发布时间】:2012-03-16 06:43:38
【问题描述】:

在用户启动长时间运行的过程时显示旋转轮进度动画 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);
        }
}

如何在流程结束之前不间断地连续显示车轮?

【问题讨论】:

  • 请发布一些关于您如何处理长时间运行的进程的代码。BackgroundWorker?开始调用?

标签: c# activity-indicator


【解决方案1】:

如果您使用框架 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();

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-10-16
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多