2

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();
}
Mr. Blond
  • 1,113
  • 2
  • 18
  • 41
  • can you include some of the logic from `RemoveTemplateButtons()`? Is any of the code in the explicitly asynchronous? – DLeh Apr 07 '15 at 20:05
  • `btn.Value` looks very suspicious here. I am nearly certain that `Remove` expects a reference to a `Button` and not its value, which is never directly a child of the canvas. – Adam Apr 07 '15 at 21:50
  • If the value is `Button` then it could cause some weird behaviour? @codesparkle – Mr. Blond Apr 08 '15 at 18:32
  • I found the solution.. That method `CaptureScreenAndSave` needs to be placed inside `Application.Current.Dispatcher` and set priority to `ApplicationIdle`. More info - [stackoverflow.com](http://stackoverflow.com/questions/31506016/how-to-know-when-window-control-styling-completed/31506946?noredirect=1#comment50977053_31506946) – Mr. Blond Jul 20 '15 at 22:37

0 Answers0