-1
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
}

上記のコードは、2 つの grdMain.Dispatcher.Invoke(...); をコメントアウトした場合に完全に機能します。*.Set(); をコメントアウトすると、完全に機能します。および *.WaitOne(); でもなんで?両方必要です^^。理解できません...

4

2 に答える 2