Displaying Please Wait Window on a SeperateThread

后端 未结 1 1858
别跟我提以往
别跟我提以往 2021-01-26 08:03

I have a method that exports the contents of my DataGrid to a CSV file. I\'m trying to display a Window that has an animation on it to ask

相关标签:
1条回答
  • 2021-01-26 08:22

    You need to move the creation of the window into the new Thread, use ShowDialog to make sure it blocks and close it via its own Dispatcher.

    PleaseWaitWindow waitWindow = null;
    
    var newWindowThread = new Thread(() =>
        {
            waitWindow = new PleaseWaitWindow();
            waitWindow.ShowDialog();
        }
    );
    
    newWindowThread.SetApartmentState(ApartmentState.STA);
    newWindowThread.Start();
    
    ExcelExport();
    
    waitWindow.Dispatcher.BeginInvoke(new Action(() =>
    {
        waitWindow.Close();
    }));
    

    Just make sure waitWindow is created before trying to close it, some kind of IPC barrier would be good here. For Example (quick and dirty):

    PleaseWaitWindow waitWindow = null;
    AutoResetEvent loaded = new AutoResetEvent(false);
    
    var newWindowThread = new Thread(() =>
        {
            waitWindow = new PleaseWaitWindow();
            loaded.Set();
            waitWindow.ShowDialog();
        });
    
    newWindowThread.SetApartmentState(ApartmentState.STA);
    newWindowThread.Start();
    
    ExcelExport();
    
    loaded.WaitOne();
    
    waitWindow.Dispatcher.BeginInvoke(new Action(() =>
    {
        waitWindow.Close();
    }));
    
    0 讨论(0)
提交回复
热议问题