1

すべて、失敗ToolStripMenuを表示するためにを更新したいと思います。SqlConnectionエラーメッセージをしばらくの間timeToWaitMs(ミリ秒単位で)表示し、しばらくして操作を行った後、UIを正常な状態に更新します。現在私はやっています(いくつかの不必要な詳細は削除されています)

public void ShowErrorWithReturnTimer(string errorMessage, int timeToWaitMs = 5000)
{
    // Update the UI (and images/colors etc.).
    this.toolStripLabelState.Text = errorMessage;

    // Wait for timeToWait and return to the default UI.
    Task task = null;
    task = Task.Factory.StartNew(() =>
        {
            task.Wait(timeToWaitMs);
        });

    // Update the UI returning to the valid connection.
    task.ContinueWith(ant =>
        {
            try
            {
                // Connection good to go (retore valid connection update UI etc.)!
                this.toolStripLabelState.Text = "Connected";
            }
            finally
            {
                RefreshDatabaseStructure();
                task.Dispose();
            }
        }, CancellationToken.None,
            TaskContinuationOptions.None,
            mainUiScheduler);
}

私が抱えている問題は、が表示されるtask.Wait(timeToWaitMs);原因になっているCursors.WaitCursorことです-これは望ましくありません。エラーメッセージを一定期間表示させた後、エラー以外の状態に戻すにはどうすればよいですか?

御時間ありがとうございます。

4

2 に答える 2

5

ここではタスクをまったく使用しません。少なくとも、C#5の非同期機能がなければ、次のように記述できます。

await Task.Delay(millisToWait);

しかし、それが得られるまでは、UIに適したタイマーを使用します(例:System.Windows.Forms.Timerまたは)System.Windows.Threading.DispatcherTimer。タイマーの「ティック」ハンドラーとして、現在取得しているものを継続として使用し、適切にスケジュールします。

于 2012-11-14T12:46:03.183 に答える
1

task.Wait()の代わりにTimerを使用できます。一定時間待機させることができます。タイマーが作動すると、コールバックは更新を開始できます。

var timer = new Timer(timeToWaitMs);
timer.Elapsed += (s, e) =>
                              {
                                  timer.Stop();
                                  UpdateValidConnection();
                              };



private void UpdateValidConnection()
{
    Task.Factory.StartNew(() =>
    {
        try             
        {
            this.toolStripLabelState.Text = "Connected";
        }
        finally
        {
            RefreshDatabaseStructure();
        }
    }, CancellationToken.None, TaskCreationOptions.None, mainUiScheduler);
}
于 2012-11-14T13:28:50.453 に答える