private void WaitForDriveToBecomeReady()
{
AutoResetEvent syncEvent = new AutoResetEvent(false); //set wait signal to use later
//dispatcher to be able to change stuff in xaml from within thread
Action action1 = new Action(delegate() { grdMain.Children.Add(notification); });
Action action2 = new Action(delegate() { grdMain.Children.Remove(notification); });
Thread restoreThread1 = new Thread(()=>{
grdMain.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, action1); //show a notification
Thread.Sleep(1500); //sleep a bit...
grdMain.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, action2); //hide a notification
syncEvent.Set(); //signal to continue at *.WaitOne()
});
restoreThread1.Start();
syncEvent.WaitOne(); //let main thread wait until *.Set(); is called
}
如果你注释掉两个grdMain.Dispatcher.Invoke(...);上面的代码是完美的。如果你注释掉* .Set(),它也可以运行perfekt;和* .WaitOne(); 但为什么呢?我需要两个^^。我不明白......
答案 0 :(得分:3)
假设在Dispatcher的线程上调用WaitForDriveToBecomeReady
,您明确地引入了死锁。
考虑执行过程
syncEvent.WaitOne()
,该线程现在被阻塞,直到该事件设置为Dispatcher.Invoke
;这会在Dispatcher的队列中放入一条消息,并等待它处理它(在主线程上)因此,主线程被阻塞,等待最终由第二个线程设置的事件。并且您阻止第二个线程等待主线程处理消息。课本死锁。
一般来说,等待UI线程是不好的;这样很容易出现死锁,但即使它有效,你仍然会阻止UI更新,创建一个无响应的程序。基于上面的代码段,很难说如何最好地重新组织代码,这样你就不必阻止UI线程,但似乎这个概念(准备一个驱动器,并在它准备就绪后做一些事情)将是一个候选者对于asynchronous method。
答案 1 :(得分:0)
我终于有时间继续阅读有关异步和等待的更多信息。感谢@Jacob指出问题所在。
以下是我的工作异常代码,只要您没有连接驱动器就会显示Toast通知。
//the method
public async Task WaitForDriveAsync(string path, string waitingToastText)
{
int width = 300;
int height = 125;
TextBlock toastTextBlock = new TextBlock() { Text = waitingToastText, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, FontSize = 23, Width = (width - 15), TextWrapping = TextWrapping.WrapWithOverflow };
Grid notification = new Grid();
notification.Width = width;
notification.Height = height;
notification.Background = Brushes.Red;
notification.Margin = new System.Windows.Thickness(0, 27, 0.4, 0);
notification.VerticalAlignment = VerticalAlignment.Top;
notification.HorizontalAlignment = HorizontalAlignment.Right;
notification.Children.Add(toastTextBlock);
grdMain.Children.Add(notification);
while (!Directory.Exists(path))
{
await Task.Delay(1000);
}
grdMain.Children.Remove(notification);
}
//to call it
private async void btnBackupNow_Click(object sender, RoutedEventArgs e)
{
await WaitForDriveAsync(@"B:\", "Please connect your drive.");
}