WPF Take screenshot from other theme besides main theme

I have a thread that is listening for commands for my WPF application. If a WPF application receives a command to take a screenshot, the task is passed to "screenshotService". I found som code to take a screenshot somewhere on interweb seems to work, but I didn't think about it .... I can't take this screenshot from another thread by providing this exception:

{"This API was accessed with arguments from the wrong context."}

      

It remains to say that the caption of my screenshot method takes the UIElement from the UI, this grid is always one and is placed in the constructor of the takeScreenshot method.

How do I go and take this screenshot?

+2


source to share


1 answer


Use the dispatcher or BackgroundWorker to complete the job:



ThreadStart start = delegate()
{
   Dispatcher.Invoke(DispatcherPriority.Normal, 
                new Action<string>(TakeScreenshot), 
                "From Other Thread");
};

new Thread(start).Start();







BackgroundWorker _backgroundWorker = new BackgroundWorker();

_backgroundWorker.DoWork += _backgroundWorker_TakeScreenshot;


_backgroundWorker.RunWorkerAsync(5000);

void _backgroundWorker_TakeScreenshot(object sender, DoWorkEventArgs e)
{
}

      

+1


source







All Articles