I have an application and I want to check through a Task if the user opens other applications and in that case minimize my application (but so that the task can continue using it).
The problem is that it throws an exception
System.InvalidOperationException: 'El subproceso que realiza la llamada no puede obtener acceso a este objeto porque el propietario es otro subproceso.'
I understand that being in a thread can't manage the main process, but I don't know how to fix it.
Task checkProcess = new Task(() =>
{
if( Process.GetProcesses().Count() > numOfProcess)
WindowState = WindowState.Minimized;
});
checkProcess.Start();
Solution:
Adding
this.Dispatcher.Invoke
to whatever you want to do works perfectly with no exceptions. +Info Dispatcher Class