How to: Update the User Interface

After you implement a command-handling scheme in your VSPackage, you can add code to update the user interface (UI) with the state of your new commands.

In a typical Win32 application, the command set can be continuously polled and the state of individual commands can be adjusted as the user views them. However, because the Visual Studio shell can host an unlimited number of VSPackages, extensive polling might decrease responsiveness, especially polling across interop assemblies between managed code and COM. 

To update the UI

  • Do one of the following:

    • Call the UpdateCommandUI method.

      An IVsUIShell interface can be obtained from the SVsUIShell service, as follows:

      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 non-zero (TRUE), then the update is performed synchronously and immediately. The Visual Studio architects recommend that you pass zero (FALSE) for this parameter to help maintain good performance. If you want to avoid caching, apply the DontCache flag when you create the command in the command table configuration file. Nevertheless, use the flag cautiously or performance might decrease. For more information about this flag, see BUTTONS_BEGIN – BUTTONS_END.

    • In VSPackages that host an ActiveX control by using the in-place activation model in a window, it might be more convenient to use the UpdateUI method. The methods UpdateCommandUI, in the IVsUIShell interface, and UpdateUI, in the IOleInPlaceComponentUIManager interface, are functionally equivalent. Both cause the environment to re-query the state of all commands. Typically, an update is not performed immediately. Instead, an update is delayed until idle time. The shell caches the command state to help maintain good performance. If you want to avoid caching, apply the DontCache flag when you create the command in the command table configuration file. Nevertheless, use the flag cautiously because performance might decrease if too many elements are not being cached. For more information about this flag, see BUTTONS_BEGIN – BUTTONS_END.

      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.

See Also

Concepts

How VSPackages Add User Interface Elements to the IDE

Implementing Commands By Using Interop Assemblies