Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 屏幕截图是在从窗口中移除按钮之前拍摄的_C#_Wpf_Multithreading_Task_System.io.file - Fatal编程技术网

C# 屏幕截图是在从窗口中移除按钮之前拍摄的

C# 屏幕截图是在从窗口中移除按钮之前拍摄的,c#,wpf,multithreading,task,system.io.file,C#,Wpf,Multithreading,Task,System.io.file,任务: 在窗口上有几个按钮显示。有一段时间它们应该从屏幕上删除。在完全移除/隐藏这些按钮后,才应拍摄屏幕截图 问题: 屏幕截图是在按钮从屏幕上移除之前拍摄的 其他信息: 由于在后台有一个长时间运行的操作,并且我需要保持UI的响应性,所以我使用异步任务调度程序。调用用于在多线程环境中使用控件RemoveTemplateButtons()基本上只是从屏幕上删除所有按钮CaptureScreenAndSave()创建屏幕截图并将其保存到文件夹中。还有一件事需要注意,调试时,当断点退出else语句时,按

任务:
在窗口上有几个按钮显示。有一段时间它们应该从屏幕上删除。在完全移除/隐藏这些按钮后,才应拍摄屏幕截图

问题:
屏幕截图是在按钮从屏幕上移除之前拍摄的

其他信息:
由于在后台有一个长时间运行的操作,并且我需要保持UI的响应性,所以我使用异步任务<代码>调度程序。调用用于在多线程环境中使用控件
RemoveTemplateButtons()
基本上只是从屏幕上删除所有按钮
CaptureScreenAndSave()
创建屏幕截图并将其保存到文件夹中。还有一件事需要注意,调试时,当断点退出
else
语句时,按钮将被删除。

代码:

//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();
编辑-删除模板按钮方法

private void RemoveTemplateButtons()
{
     //in case if image searching happens more than once
     foreach (var btn in templButtonDictionary)
     {
         canvasBackground.Children.Remove(btn.Value);
     }
     templButtonDictionary.Clear();
}

是否可以包含
RemoveTemplateButtons()
中的一些逻辑?是否有显式异步的代码?
btn.Value
在这里看起来非常可疑。我几乎可以肯定,
Remove
期望引用一个
按钮,而不是它的值,它从来不是画布的子对象。如果值是
按钮
,那么它可能会导致一些奇怪的行为@我找到了解决办法。。该方法
capturescreen和save
需要放在
Application.Current.Dispatcher
中,并将优先级设置为
ApplicationIdle
。更多信息-