How do I know when Visual Studio finishes the initialization process
After a long time my nineteenth How do I is up.
Scenario
Sometimes we need to initialize services in the Package.Initialize() method and these services have a dependency on the STE/DTE or other services. The problem is that if the shell is not fully initialized by that moment the SDTE/DTE GetService call will return null. The solution to this problem is to initialize this kind of services when the shell is fully initialized. The focus of this How do I tackles this scenario.
Interfaces and classes needed
Code snippet
public
sealed
class
VSPackage1Package : Package, IVsShellPropertyEvents
{
private uint shellCookie;
protected override void Initialize()
{
base.Initialize();
IVsShell shellService = this.GetService(typeof(SVsShell)) as IVsShell;
ErrorHandler.ThrowOnFailure(
shellService.AdviseShellPropertyChanges(this, out shellCookie));
}
public int OnShellPropertyChange(int propid, object var)
{
if(propid == (int)__VSSPROPID.VSSPROPID_Zombie)
{
if((bool)var == false)
{
//At this point the environment is fully loaded and initialized
//Lets initialize our services
IVsShell shellService = this.GetService(typeof(SVsShell)) as IVsShell;
if(shellService != null)
{
ErrorHandler.ThrowOnFailure(
shellService.UnadviseShellPropertyChanges(this.shellCookie));
}
this.shellCookie = 0;
}
}
return VSConstants.S_OK;
}
}
Assemblies needed
- Microsoft.VisualStudio.Shell.Interop
- Microsoft.VisualStudio.Shell
Stay tuned,
Pablo