Changes in the main process from a task

1

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 I can still use it for that purpose).

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 subprocess can not manage the main process, but I do not know how to solve it.

Task checkProcess = new Task(() =>
{
   if( Process.GetProcesses().Count() > numOfProcess)
       WindowState = WindowState.Minimized;
});
checkProcess.Start();
    
asked by jooooooooota 24.07.2017 в 12:29
source

1 answer

2

Solution:

Task checkProcess = new Task(() =>
{
   if( Process.GetProcesses().Count() > numOfProcess)
       this.Dispatcher.Invoke(() =>
        {
           WindowState = WindowState.Minimized;
        });
});
checkProcess.Start()

With adding this.Dispatcher.Invoke to what you want to do it works perfectly without exceptions. + Info Class Dispatcher

    
answered by 24.07.2017 / 12:45
source