<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Check if command is running and set button state with IExternalCommandAvailability in Revit API Forum</title>
    <link>https://forums.autodesk.com/t5/revit-api-forum/check-if-command-is-running-and-set-button-state-with/m-p/12825040#M4850</link>
    <description>&lt;P&gt;Hello Revit API friends,&lt;BR /&gt;&lt;BR /&gt;I have a general question about handling deactivating and reactivating a ribbon button.&lt;BR /&gt;I want to deactivate the ribbon button while the command is running, so it can´t be run multiple times.&lt;BR /&gt;But I´m not quite sure what´s the best way to make sure the button is reactivated in any case that can happen.&lt;BR /&gt;&lt;BR /&gt;Please see the following simple example of a command that is starting a wpf form.&lt;BR /&gt;- I use the IExternalCommandAvailability right at the start of my command.&lt;BR /&gt;- I use a closing eventhandler that triggers the IExternalCommandAvailability if the form is closed.&lt;BR /&gt;- I use&amp;nbsp;IExternalCommandAvailability in a catch block.&lt;BR /&gt;&lt;BR /&gt;Is this a 100% save method to amke sure my button is set active again also if error occure in my command? Are there other options for this? Can I check somehow if a command is running or if a wpf form is open?&lt;BR /&gt;&lt;BR /&gt;Unfortunately I can not use a finally block because it will run instantly as the command seems to run async.&lt;BR /&gt;&lt;BR /&gt;Appreciate any help on this topic!&lt;BR /&gt;Best regards!&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;	[Transaction(TransactionMode.Manual)]
	internal class Command : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			try
			{
				CommandAvailability.IsCommandRunning = true;
				
				var mainWindow = new MainWindow(commandData.Application);
				mainWindow.Show();

				mainWindow.Closed += (s, e) =&amp;gt; CommandAvailability.IsCommandRunning = false;
				return Result.Succeeded;
			}
			catch
			{
				CommandAvailability.IsCommandRunning = false;
				return Result.Succeeded;
			}

		}
	}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;	public class CommandAvailability : IExternalCommandAvailability
	{
		private static bool isCommandRunning = false;

		public static bool IsCommandRunning
		{
			get { return isCommandRunning; }
			set { isCommandRunning = value; }
		}

		public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
		{
			return !isCommandRunning;
		}
	}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 07 Jun 2024 11:08:11 GMT</pubDate>
    <dc:creator>gerhard.pawlat</dc:creator>
    <dc:date>2024-06-07T11:08:11Z</dc:date>
    <item>
      <title>Check if command is running and set button state with IExternalCommandAvailability</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/check-if-command-is-running-and-set-button-state-with/m-p/12825040#M4850</link>
      <description>&lt;P&gt;Hello Revit API friends,&lt;BR /&gt;&lt;BR /&gt;I have a general question about handling deactivating and reactivating a ribbon button.&lt;BR /&gt;I want to deactivate the ribbon button while the command is running, so it can´t be run multiple times.&lt;BR /&gt;But I´m not quite sure what´s the best way to make sure the button is reactivated in any case that can happen.&lt;BR /&gt;&lt;BR /&gt;Please see the following simple example of a command that is starting a wpf form.&lt;BR /&gt;- I use the IExternalCommandAvailability right at the start of my command.&lt;BR /&gt;- I use a closing eventhandler that triggers the IExternalCommandAvailability if the form is closed.&lt;BR /&gt;- I use&amp;nbsp;IExternalCommandAvailability in a catch block.&lt;BR /&gt;&lt;BR /&gt;Is this a 100% save method to amke sure my button is set active again also if error occure in my command? Are there other options for this? Can I check somehow if a command is running or if a wpf form is open?&lt;BR /&gt;&lt;BR /&gt;Unfortunately I can not use a finally block because it will run instantly as the command seems to run async.&lt;BR /&gt;&lt;BR /&gt;Appreciate any help on this topic!&lt;BR /&gt;Best regards!&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;	[Transaction(TransactionMode.Manual)]
	internal class Command : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			try
			{
				CommandAvailability.IsCommandRunning = true;
				
				var mainWindow = new MainWindow(commandData.Application);
				mainWindow.Show();

				mainWindow.Closed += (s, e) =&amp;gt; CommandAvailability.IsCommandRunning = false;
				return Result.Succeeded;
			}
			catch
			{
				CommandAvailability.IsCommandRunning = false;
				return Result.Succeeded;
			}

		}
	}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;	public class CommandAvailability : IExternalCommandAvailability
	{
		private static bool isCommandRunning = false;

		public static bool IsCommandRunning
		{
			get { return isCommandRunning; }
			set { isCommandRunning = value; }
		}

		public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
		{
			return !isCommandRunning;
		}
	}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2024 11:08:11 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/check-if-command-is-running-and-set-button-state-with/m-p/12825040#M4850</guid>
      <dc:creator>gerhard.pawlat</dc:creator>
      <dc:date>2024-06-07T11:08:11Z</dc:date>
    </item>
    <item>
      <title>Re: Check if command is running and set button state with IExternalCommandAvailability</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/check-if-command-is-running-and-set-button-state-with/m-p/12825286#M4851</link>
      <description>&lt;P&gt;Hmmm, I don't get it why you are using &lt;STRONG&gt;ExternalCommandAvailability&lt;/STRONG&gt;?&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I have some code that does some similar, only allow to open one wpf, and if already open. I'm using a static inside the Command, makes more sense in my mind.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
    private static SimpleView window;
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elementSet)
    {
        UIApplication uiapp = commandData.Application;

        if (window is null)
        {
            window = new SimpleView();
            window.Closed += (s, e) =&amp;gt; { window = null; };
            window.Show();
        }
        window?.Activate();

        return Result.Succeeded;
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my sample if you press the command again the window gonna be activated if already opened.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2024 12:11:17 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/check-if-command-is-running-and-set-button-state-with/m-p/12825286#M4851</guid>
      <dc:creator>ricaun</dc:creator>
      <dc:date>2024-06-07T12:11:17Z</dc:date>
    </item>
    <item>
      <title>Re: Check if command is running and set button state with IExternalCommandAvailability</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/check-if-command-is-running-and-set-button-state-with/m-p/12825442#M4852</link>
      <description>&lt;P&gt;Hello &lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/4176855"&gt;@ricaun&lt;/a&gt;&amp;nbsp;and thanks for your reply!&lt;BR /&gt;&lt;BR /&gt;Until now I used the same aproach that you are using.&lt;BR /&gt;&lt;BR /&gt;But my requirements now are to have the button greyed out and non clickable while the command is running. This is why i went for using&amp;nbsp;IExternalCommandAvailability. I apologize that I didn´t explain this correct before.&lt;BR /&gt;&lt;BR /&gt;I was hoping that the interface will give me informations on the state of a command, but it´s not. I have to tell the interface if the command should be available or not.&lt;BR /&gt;&lt;BR /&gt;My question is now if it is possible to deactivate (grey out) the button while the command is running, without risking that the button will stay deactivated if my command fails. I´m not shure if I can trust the catch block and eventhandler I´m using now.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2024 13:32:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/check-if-command-is-running-and-set-button-state-with/m-p/12825442#M4852</guid>
      <dc:creator>gerhard.pawlat</dc:creator>
      <dc:date>2024-06-07T13:32:39Z</dc:date>
    </item>
    <item>
      <title>Re: Check if command is running and set button state with IExternalCommandAvailability</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/check-if-command-is-running-and-set-button-state-with/m-p/12825578#M4853</link>
      <description>&lt;P&gt;I get it you want to disable the command with your window is open.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One thing you could do to make your command better is to join the &lt;STRONG&gt;IExternalCommand&lt;/STRONG&gt; and &lt;STRONG&gt;IExternalCommandAvailability&lt;/STRONG&gt; in the same class. So you don't need to juggle the static propriety between classes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ricaun_0-1717769532749.gif" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1373262i5F54B340AA3E816C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ricaun_0-1717769532749.gif" alt="ricaun_0-1717769532749.gif" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Here is a sample.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using RevitAddin.Forum.Views;
using System;

namespace RevitAddin.Forum.Revit.Commands
{
    [Transaction(TransactionMode.Manual)]
    public class CommandGrey : IExternalCommand, IExternalCommandAvailability
    {
        private static SimpleView window;
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elementSet)
        {
            UIApplication uiapp = commandData.Application;

            try
            {
                window = new SimpleView();
                window.Closed += (s, e) =&amp;gt; { window = null; };
                window.Show();
            }
            catch (Exception)
            {
                window = null;
            }

            return Result.Succeeded;
        }

        public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
        {
            return window is null;
        }
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just need to register the command and availability with the same full class name.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I believe the try/catch gonna work fine, only gonna grey forever stat open or the event &lt;EM&gt;&lt;STRONG&gt;Closed&lt;/STRONG&gt; &lt;/EM&gt;not trigger.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2024 14:19:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/check-if-command-is-running-and-set-button-state-with/m-p/12825578#M4853</guid>
      <dc:creator>ricaun</dc:creator>
      <dc:date>2024-06-07T14:19:06Z</dc:date>
    </item>
    <item>
      <title>Re: Check if command is running and set button state with IExternalCommandAvailability</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/check-if-command-is-running-and-set-button-state-with/m-p/12836825#M4854</link>
      <description>&lt;P&gt;Thanks for your help&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/4176855"&gt;@ricaun&lt;/a&gt;&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2024 09:00:00 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/check-if-command-is-running-and-set-button-state-with/m-p/12836825#M4854</guid>
      <dc:creator>gerhard.pawlat</dc:creator>
      <dc:date>2024-06-13T09:00:00Z</dc:date>
    </item>
  </channel>
</rss>

