The problem is in VirtualDesktopManager CloseAll method. It is used when I want all windows to be closed, so I can simulate the restart of the application. The behaviour of this method is very weird sometimes.
If it is called normally with command in the viewmodel:
this.GetService<IVirtualDesktopManager>().CloseAll();
It completely hangs the application due to parallelization and it stays in loop in method (VirtualDesktopManager.cs):
private void DeactivateAll()
{
this.Desktops.AsParallel().ForAll(vd => vd.Deactivate());
//this.Desktops.ForEach(vd => vd.Deactivate()); // This is the solution to the hang of the application
}
The bottom line was added by me during testing and it fixes the spinlock. However the method has another problem. It disables any new windows opening, except modal windows, no windows, nothing.