Unable to call sequential commands together

Unable to call sequential commands together

soonhui
Advisor Advisor
691 Views
8 Replies
Message 1 of 9

Unable to call sequential commands together

soonhui
Advisor
Advisor

I have the below code:

 [CommandMethod(nameof(ConsecutiveCommand))]
 public void ConsecutiveCommand()
 {
     Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument
         .Editor.Command("_AeccAddNetworkPipeTable");
     Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument
         .Editor.Command("_AeccAddNetworkStructTable");
 }

 

I want the first command to finish executing before execute the second one. But then, it seems that only the first one is executed but not the second one.

 

Both of the commands involve user actions, like depending on user input to place the table. I'm not sure whether this is what causing the second command to be "overlooked".

 

Any ideas?

##########

Ngu Soon Hui

##########

I'm the Benevolent Dictator for Life for MiTS Software. Read more here


I also setup Civil WHIZ in order to share what I learnt about Civil 3D
0 Likes
Accepted solutions (1)
692 Views
8 Replies
Replies (8)
Message 2 of 9

_gile
Consultant
Consultant
Accepted solution

Hi,

What about using CommandAsync instead?

[CommandMethod(nameof(ConsecutiveCommand))]
public static async void ConsecutiveCommand()
{
    var ed = Application.DocumentManager.MdiActiveDocument.Editor;
    await ed.CommandAsync("_AeccAddNetworkPipeTable");
    while (((string)AcCoreAp.GetSystemVariable("CMDNAMES")).Contains("AeccAddNetworkPipeTable", StringComparison.CurrentCultureIgnoreCase))
    {
        try { await ed.CommandAsync(Editor.PauseToken); }
        catch { break; }
    }
    ed.Command("_AeccAddNetworkStructTable");
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 9

soonhui
Advisor
Advisor

So essentially if I have more commands to be called sequentially, I just need to chain the code accordingly?

##########

Ngu Soon Hui

##########

I'm the Benevolent Dictator for Life for MiTS Software. Read more here


I also setup Civil WHIZ in order to share what I learnt about Civil 3D
0 Likes
Message 4 of 9

_gile
Consultant
Consultant

Theoretically, if the invoked command requires pauses for user input, you would need to use CommandAsync with a while loop as in the example above if the number of user inputs is not fixed, or the number of pauses required if it is known, to ensure that the command is completed before calling another one.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 9

soonhui
Advisor
Advisor

@_gile , your solution involving CommandAsync work, but I just wonder about Command method that I use in the original thread, because I would think that it must also work, as per your comment in another forum post here.

 


@_gile wrote:
With the Editor.Command() method, all the 'while stuff' is implicit, and the program will wait for the command completed:


 

But why the Command didn't work? By right it should wait for the user input and hide all the while stuff, right?

##########

Ngu Soon Hui

##########

I'm the Benevolent Dictator for Life for MiTS Software. Read more here


I also setup Civil WHIZ in order to share what I learnt about Civil 3D
0 Likes
Message 6 of 9

_gile
Consultant
Consultant

@soonhui  a écrit :

@_gile , your solution involving CommandAsync work, but I just wonder about Command method that I use in the original thread, because I would think that it must also work, as per your comment in another forum post here.

 


@_gile wrote:
With the Editor.Command() method, all the 'while stuff' is implicit, and the program will wait for the command completed:


 

But why the Command didn't work? By right it should wait for the user input and hide all the while stuff, right?


The topic of TheSwamp that you mention (more than 10 years ago) was an attempt to supplement Kean Walmsley's topic explaining the differences between Command (subroutine) and CommandAsync (coroutine) with my own experiences. Based on the (too few) tests I had done at the time, it seemed to me that Command could also be called incompletely. The present topic (i.e., your experience) shows that my assertion at the time was inaccurate, hence my response with a return to the theory put forward by Kean (i.e., use CommandAsync when "you don't have all the arguments to provide to the command").

 

That said, for your purpose, a more robust method might be the following:

private static bool IsCommandActive(string cmdName) =>
    ((string)AcCoreAp.GetSystemVariable("CMDNAMES"))
    .Contains(cmdName, StringComparison.OrdinalIgnoreCase);

[CommandMethod(nameof(ConsecutiveCommand))]
public static async void ConsecutiveCommand()
{
    var ed = Application.DocumentManager.MdiActiveDocument.Editor;
    try
    {
        await ed.CommandAsync("_AeccAddNetworkPipeTable");
        while (IsCommandActive("AeccAddNetworkPipeTable"))
        {
            try { await ed.CommandAsync(Editor.PauseToken); }
            catch { return; }
        }
        await ed.CommandAsync("_AeccAddNetworkStructTable");
        while (IsCommandActive("AeccAddNetworkStructTable"))
        {
            try { await ed.CommandAsync(Editor.PauseToken); }
            catch { return; }
        }
    }
    catch(Autodesk.AutoCAD.Runtime.Exception ex)
    {
        ed.WriteMessage($"\nError: {ex.Message}");
    }
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 9

soonhui
Advisor
Advisor

@_gile , 

 

So it does seem that the difference in Command and CommandAsync is... what exactly? Both seem to be fire and forget...

##########

Ngu Soon Hui

##########

I'm the Benevolent Dictator for Life for MiTS Software. Read more here


I also setup Civil WHIZ in order to share what I learnt about Civil 3D
0 Likes
Message 8 of 9

_gile
Consultant
Consultant

@soonhui  a écrit :

@_gile , 

 

So it does seem that the difference in Command and CommandAsync is... what exactly? Both seem to be fire and forget...


I cannot explain it in a better English than Kean Walmsley did in this topic.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 9 of 9

soonhui
Advisor
Advisor

@_gile , my conclusion is that the Command is synchronous only in the sense that you apply it to subroutine ( still mostly command line based thing). But the moment you apply it to command that invoke UI elements like Dialog Box, it no longer applies. Agree?

##########

Ngu Soon Hui

##########

I'm the Benevolent Dictator for Life for MiTS Software. Read more here


I also setup Civil WHIZ in order to share what I learnt about Civil 3D
0 Likes