protected override void Initialize() { // Long running synchronous method call that blocks the UI thread Thread.Sleep(5000); // Adds a service synchronosly on the UI thread var callback = new ServiceCreatorCallback(CreateMyService); ((IServiceContainer)this).AddService(typeof(MyService), callback); // Synchronously requesting a service on the UI thread var dte = GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE; // Initializes the command synchronously on the UI thread MyCommand.Initialize(this, dte); }
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress <ServiceProgressData> progress) { // runs in the background thread and doesn't affect the responsiveness of the UI thread. await Task.Delay(5000); // Adds a service on the background thread AddService(typeof(MyService), CreateMyServiceAsync); // Switches to the UI thread in order to consume some services used in command initialization await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); // Query service asynchronously from the UI thread var dte = await GetServiceAsync(typeof(EnvDTE.DTE)) as EnvDTE.DTE; // Initializes the command asynchronously now on the UI thread await MyCommand.InitializeAsync(this, dte); }