1

ユーザーが実行時間の長いプロセスを開始している間、スピン ホイールの進行状況のアニメーション 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);
        }
}

プロセスが終了するまでホイールを連続して表示するにはどうすればよいですか?

4

2 に答える 2

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();
于 2012-03-16T07:36:37.097 に答える
0

進行状況の表示とタスクを同じスレッドで実行することはできません。BackgroundWorkerを使用する必要があります

GUIスレッドはProgressChangedイベントをサブスクライブし、タスクの更新が通知されます。ここから、進捗状況を適切に更新できます。タスクが終了したときのイベントもあります。

于 2012-03-16T06:51:11.827 に答える