Grabacr07 / VirtualDesktop

C# wrapper for the Virtual Desktop API on Windows 11.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't initialize with a null SynchronizationContext.Current

danielchalmers opened this issue · comments

If SynchronizationContext.Current is null, VirtualDesktopProvider.Initialize will throw an InvalidOperationException: 'The current SynchronizationContext may not be used as a TaskScheduler.' at

public Task Initialize()
=> this.Initialize(TaskScheduler.FromCurrentSynchronizationContext());

This stops a blank console app from using VirtualDesktop.

Steps to reproduce:

  • Create a new console app
  • Target Windows 10 in app.manifest and add a reference to VirtualDesktop 3 beta
  • In the main method, call VirtualDesktopProvider.Default.Initialize() (or a method that calls it for you like VirtualDesktop.GetDesktops()).

Workaround:
Call SynchronizationContext.SetSynchronizationContext first or use Initialize(TaskScheduler scheduler).