Repository: microsoft/PowerToys
File: src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Pages/ActionsListContextItem.cs (C#)

Code before:
        Title = Resources.Indexer_Command_Actions;
        Icon = Icons.Actions;
        RequestedShortcut = KeyChordHelpers.FromModifiers(alt: true, vkey: VirtualKey.A);
        this.fullPath = fullPath;
        UpdateMoreCommands();
    }

    public bool AnyActions() => actions.Count != 0;

    private void ActionCatalog_Changed(global::Windows.AI.Actions.Hosting.ActionCatalog sender, object args)
    {
        UpdateMoreCommands();
    }

    private void UpdateMoreCommands()
    {
        try
        {
            if (actionRuntime == null)
            {
                actionRuntime = ActionRuntimeFactory.CreateActionRuntime();
                Task.Delay(500).Wait();
            }

            actionRuntime.ActionCatalog.Changed -= ActionCatalog_Changed;
            actionRuntime.ActionCatalog.Changed += ActionCatalog_Changed;

            var extension = System.IO.Path.GetExtension(fullPath).ToLower(CultureInfo.InvariantCulture);
            ActionEntity entity = null;
            if (extension != null)
            {
                if (extension == ".jpg" || extension == ".jpeg" || extension == ".png")
                {
                    entity = actionRuntime.EntityFactory.CreatePhotoEntity(fullPath);
                }
                else if (extension == ".docx" || extension == ".doc" || extension == ".pdf" || extension == ".txt")
                {
                    entity = actionRuntime.EntityFactory.CreateDocumentEntity(fullPath);
                }
            }

            if (entity == null)
            {
                entity = actionRuntime.EntityFactory.CreateFileEntity(fullPath);
            }

            lock (actions)
            {
                actions.Clear();
                foreach (var actionInstance in actionRuntime.ActionCatalog.GetActionsForInputs([entity]))
                {

Review comment (refactor):
do we need to remove this event handler in our dtor?

(this is just me being mid at C# - I forget if these event handlers are smart and will get cleaned up for us or not)

Code after:
        : base(new NoOpCommand())
    {
        Title = Resources.Indexer_Command_Actions;
        Icon = Icons.Actions;
        RequestedShortcut = KeyChordHelpers.FromModifiers(alt: true, vkey: VirtualKey.A);
        this.fullPath = fullPath;
        UpdateMoreCommands();
    }

    public bool AnyActions() => actions.Count != 0;

    private void ActionCatalog_Changed(global::Windows.AI.Actions.Hosting.ActionCatalog sender, object args)
    {
        UpdateMoreCommands();
    }

    private void UpdateMoreCommands()
    {
        try
        {
            lock (UpdateMoreCommandsLock)
            {
                if (actionRuntime == null)
                {
                    actionRuntime = ActionRuntimeFactory.CreateActionRuntime();
                    Task.Delay(500).Wait();
                }

                actionRuntime.ActionCatalog.Changed -= ActionCatalog_Changed;
                actionRuntime.ActionCatalog.Changed += ActionCatalog_Changed;
            }

            var extension = System.IO.Path.GetExtension(fullPath).ToLower(CultureInfo.InvariantCulture);
            ActionEntity entity = null;
            if (extension != null)
            {
                if (extension == ".jpg" || extension == ".jpeg" || extension == ".png")
                {
                    entity = actionRuntime.EntityFactory.CreatePhotoEntity(fullPath);
                }
                else if (extension == ".docx" || extension == ".doc" || extension == ".pdf" || extension == ".txt")
                {
                    entity = actionRuntime.EntityFactory.CreateDocumentEntity(fullPath);
                }
            }

            if (entity == null)
            {
                entity = actionRuntime.EntityFactory.CreateFileEntity(fullPath);
            }