Moo*_*ght 1 c# multithreading wait task-parallel-library
全部,我想更新一个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);
}
Run Code Online (Sandbox Code Playgroud)
我task.Wait(timeToWaitMs);遇到的问题是导致Cursors.WaitCursor显示 - 我不想要这个.如何强制显示错误消息一段时间,之后我将返回非错误状态?
谢谢你的时间.
我根本不会在这里使用任务 - 至少没有C#5中的异步功能.在C#5中你可以写:
await Task.Delay(millisToWait);
Run Code Online (Sandbox Code Playgroud)
但是直到你有了它,我才会使用适合你的UI的计时器,例如System.Windows.Forms.Timer或System.Windows.Threading.DispatcherTimer.只需使用您当前获得的作为计时器"tick"处理程序的延续,并适当地安排它.
| 归档时间: |
|
| 查看次数: |
465 次 |
| 最近记录: |