How to: Update the User Interface
After implementing a command-handling scheme in your VSPackage, use the following procedure to update the user interface with the state of your new commands.
In a typical Win32 application, it is feasible to continuously poll the entire application's command set and adjust the state of individual commands as the user views them. Because of the shell's capability to host an unlimited number of VSPackages, extensive use of polling, especially polling across the interop assemblies between managed code and COM, can cause responsiveness to degrade.
To update the user interface
-
The easiest way to update the user interface is to call the UpdateCommandUI method.
An IVsUIShell interface can be obtained from the SVsUIShell service:
void UpdateUI(Microsoft.VisualStudio.Shell.ServiceProvider sp) { IVsUIShell vsShell = (IVsUIShell)sp.GetService(typeof(IVsUIShell)); if (uiShellSvc != null) { int hr = uiShellSvc.UpdateCommandUI(0); Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(hr); }; }
void UpdateUI(IServiceProvider *pSP) { CComPtr< IVsUIShell> srpShell; int hresult = pSP->QueryService(SID_SVsUIShell, IID_IVsUIShell, (void **)&srpShell); if (SUCCEEDED(hresult) && NULL != srpShell) { sprShell->UpdateCommandUI(FALSE); } }
If the single parameter of the UpdateCommandUI is nonzero (TRUE) then the update is performed synchronously and immediately. The Visual Studio architects recommended passing zero (FALSE) for this parameter due to performance issues. If you wish to avoid caching, apply the DontCache flag when you create the command in the .ctc file, but use the flag cautiously due to performance concerns. See BUTTONS_BEGIN – BUTTONS_END for a description of this flag.
-
VSPackages hosting an ActiveX control using the in-place activation model in a window may find it more convenient to use the UpdateUI method. The methods UpdateCommandUI (in the IVsUIShell interface) and UpdateUI (in the IOleInPlaceComponentUIManager interface) are functionally equivalent, causing the environment to re-query the state of all commands. Typically, the update is not performed immediately but rather delayed until idle time. The shell caches the command state for performance reasons. If you wish to avoid caching, apply the DontCache flag when you create the command in the .ctc file, but use the flag cautiously as too many elements not being cached can adversely impact the performance of the shell. See BUTTONS_BEGIN – BUTTONS_END for a description of this flag.
Note You can obtain the IOleInPlaceComponentUIManager interface by calling the QueryInterface method on an IOleComponentUIManager object or by obtaining the interface from the SOleComponentUIManager service.