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(); ,它也可以正常工作。和 *.WaitOne(); 但是为什么?我两个都需要^^。我不明白...