The task:
On the window there are several buttons showing. There is a moment when they should be removed from the screen. After those buttons are completely removed/hidden, only then the screen shot should be taken.
The problem:
Screen shot is taken before the buttons are removed from the screen.
Additional info:
As there is a long running operation in background, and I need to keep the UI responsive, I'm using asyncronous tasks. Dispatcher.Invoke
is used to be able to work with controls in multithreaded environment. RemoveTemplateButtons()
basically just removes all the buttons from the screen. CaptureScreenAndSave()
creates a screenshot and save it to the folder. Also one thing to menthion, when debugging, the buttons are removed when the breakpoint exits the else
statement.
The code:
//show the loading screen
loadingWindow.Show();
var slowTask = Task.Factory.StartNew(() =>
{
this.Dispatcher.Invoke((Action)(() =>
{
if ( /* ... */)
{
}
/* THE SCREENSHOT SHOULD BE TAKEN AFTER BUTTONS ARE REMOVED FROM SCREEN */
else
{
//remove template buttons from screen
this.RemoveTemplateButtons();
//captures screenshot of window and saves into folder
this.CaptureScreenAndSave();
}
}));
});
//long running operation
await slowTask.ContinueWith((s) =>
{
Parallel.For(0, count - 1, i =>
{
//do some work
});
});
//waits till long running operations are done
await slowTask;
//some other logic ( syncronous )
//hide loading screen
loadingWindow.Close();
EDIT- Remove template buttons method
private void RemoveTemplateButtons()
{
//in case if image searching happens more than once
foreach (var btn in templButtonDictionary)
{
canvasBackground.Children.Remove(btn.Value);
}
templButtonDictionary.Clear();
}