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
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();
}));