Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Create and add MEF commands on background thread #1548

Closed
wants to merge 5 commits into from

Conversation

jcansdale
Copy link
Collaborator

@jcansdale jcansdale commented Mar 21, 2018

What this PR does

  • Create MEF commands on the background thread
  • Allow menuService.AddCommands to be called from a b/g thread
  • Created AsyncMenuPackage that allows IMenuCommandService to be used on b/g tread
  • Make PullRequestStatusBarPackage use MEF on b/g thread before using it on Main thread

Notes

I attempted to do everything on the background thread, including calling AddCommand on the IMenuCommandService service. There had been reports of this also working on a background thread:
https://gitter.im/Microsoft/extendvs?at=5ab13986458cbde55757fe4e

Unfortunately MenuCommandService.EnsureVerbs includes a call to GetService:
https://gitter.im/Microsoft/extendvs?at=5ab141aaf3f6d24c6898259e

This can only be called on a background thread by a non-AsyncPackage, for exampe:
http://source.roslyn.io/#Microsoft.VisualStudio.LanguageServices/RoslynPackage.cs,106
(IMenuCommandService and IOleCommandTarget) are services that are created for each Package)

IMenuCommandService.AddCommands to be called form a background thread:

// The IDesignerHost and ISelectionService services are requested by MenuCommandService.EnsureVerbs().
// When called from a non-Main thread this would throw despite the fact these services don't exist.
// This override allows IMenuCommandService.AddCommands to be called form a background thread.
protected override object GetService(Type serviceType) =>
    (serviceType == typeof(ISelectionService) ||
    serviceType == typeof(IDesignerHost)) ? null : base.GetService(serviceType);

This allows us to do all menu related initialization on a b/g thread!

@jcansdale jcansdale changed the title Create MEF commands on background thread Create and add MEF commands on background thread Mar 21, 2018
@jcansdale jcansdale requested a review from grokys March 21, 2018 10:29

namespace GitHub.Services.Vssdk
{
public abstract class AsyncMenuPackage : AsyncPackage
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs some (preferably gramatical) xmldoc comments. 😉

@jcansdale
Copy link
Collaborator Author

Made obsolete by #1569.

@jcansdale jcansdale closed this Apr 3, 2018
@jcansdale jcansdale deleted the fixes/1544-background-MEF branch April 3, 2018 19:50
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant