全部我想更新ToolStripMenu
以顯示SqlConnection
失敗。我想要顯示一段時間的錯誤消息timeToWaitMs
(以毫秒爲單位),然後在一段時間和某些操作之後將界面刷新回正常狀態。目前,我做(有一些非必要的細節,去掉)在更新用戶界面前強制任務等待
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
- 我不想這樣。如何強制顯示錯誤消息一段時間,之後我又回到非錯誤狀態?
謝謝你的時間。
只要刪除'task.Wait(timeToWaitMs);'?開始一項任務只是讓它等待是毫無意義的。 –