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
}
The above code works perfect IF you comment out the two grdMain.Dispatcher.Invoke(...);. It also works perfekt if you comment out the *.Set(); and *.WaitOne(); But WHYYYY? I need both ^^. I don't get it...
I finally had time to continue read more about async and await. Thanks @Jacob for pointing out what was the problem.
Here is my working async code for a toast notification that appears as long as you don't connect your drive.