Issues with setting _dte.Events.CommandEvents.BeforeExecute callback -it's not running.

Jim Locigno 20 Reputation points
2023-12-30T16:38:48.02+00:00

I am having trouble even getting the InitalizeAysnc method to even run, break points are not getting hit. I assume this is why my custom code for BeforeExecute isn't running.

I have searched google and VSIX github repos to no end and can't figure this out.

Here is my latest attempt:

> global using Community.VisualStudio.Toolkit;
> global using Microsoft.VisualStudio.Shell;
> global using System;
> global using Task = System.Threading.Tasks.Task;
> using EnvDTE;
> using EnvDTE80;
> using Microsoft;
> using System.Diagnostics;
> using System.IO;
> using System.Runtime.InteropServices;
> using System.Threading;
> 
> namespace VSIXProject13
> {
>     [PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
>     [InstalledProductRegistration(Vsix.Name, Vsix.Description, Vsix.Version)]
>     [ProvideMenuResource("Menus.ctmenu", 1)]
>     [Guid(PackageGuids.VSIXProject13String)]
>     public sealed class VSIXProject13Package : ToolkitPackage
>     {
> 
>         public const string PackageGuidString = "bcf67d7a-8e10-4fe5-af47-2fa32560656a";
>         private DTE2 _dte;
> 
> 
>         protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
>         {
>         
>             await this.RegisterCommandsAsync();
> 
>             await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
> 
>             await base.InitializeAsync(cancellationToken, progress);
> 
>             _dte = (DTE2)await GetServiceAsync(typeof(DTE));
>             Assumes.Present(_dte);
>        
>             // Hook up event handlers for before and after command execution
>             _dte.Events.CommandEvents.BeforeExecute += OnBeforeCommandExecute;
>             _dte.Events.CommandEvents.AfterExecute += OnAfterCommandExecute; _dte = (DTE2)GetService(typeof(DTE));
> 
> 
> 
>         }
> 
> 
>     private void OnBeforeCommandExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
>         {
>             var sw = new StreamWriter(@"c:\test\vsix.log");
>             sw.AutoFlush = true;
>             sw.WriteLine("here-1\n");
>             sw.Close();
>             // Add custom code to run before the built-in command execution
>             // Example: Console.WriteLine("Before executing command: " + _dte.Commands.Item(Guid, ID).Name);
>         }
> 
>         /// <summary>
>         /// Event handler for after command execution.
>         /// </summary>
>         private void OnAfterCommandExecute(string Guid, int ID, object CustomIn, object CustomOut)
>         {
>             // Add custom code to run after the built-in command execution
>             // Example: Console.WriteLine("After executing command: " + _dte.Commands.Item(Guid, ID).Name);
>             Debug.Print("We are in the OnAfterCommandExecute event");
>         }
> 
>         /// <summary>
>         /// Event handler for when the IDE begins to shut down.
>         /// </summary>
>         private void OnBeginShutdown()
>         {
>             // Clean up resources and unregister event handlers
>             _dte.Events.DTEEvents.OnBeginShutdown -= OnBeginShutdown;
>             _dte.Events.CommandEvents.BeforeExecute -= OnBeforeCommandExecute;
>             _dte.Events.CommandEvents.AfterExecute -= OnAfterCommandExecute;
>         }
>     }
> }
> 
> ```

Visual Studio Extensions
Visual Studio Extensions
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Extensions: A program or program module that adds functionality to or extends the effectiveness of a program.
226 questions
0 comments No comments
{count} votes

Accepted answer
  1. gekka 9,586 Reputation points MVP
    2023-12-31T02:48:17.2+00:00

    If your command appears in the menu, your VSIX is recognized by VisualStudio.

    However, AllowsBackroundLoading=true delays class initialization.
    Delays of more than 1 minute are frequent.

    Setting it to false for debugging purposes can speed up loading.

    BackgroundLoading=true actually also requires that ProvideAutoLoadAttribute be added to Package.

    ・・・
    [ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.SolutionExists, PackageAutoLoadFlags.BackgroundLoad)]
    public sealed class VSIXProject13Package : ToolkitPackage
    {
    ・・・
    

     

    And, if you don't keep _dte.Events.CommandEvents in the field, it will be deleted by garbage collector and you won't be able to receive events.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.