Editor CommandAsync Error

Editor CommandAsync Error

Anonymous
Not applicable
3,429 Views
5 Replies
Message 1 of 6

Editor CommandAsync Error

Anonymous
Not applicable

Hello -

 

I am trying to call a command with CommandAsync and I keep getting an error.  I need the code to keep running before the command finishes.  I have been doing a lot of reading and trying different things but I'm stuck.  This is what I am using:

 

ed.CommandAsync("Command","Parameter","Parameter")

 

it does what it is supposed to, but gives this exception after:

System.NullReferenceException: Object reference not set to an instance of an object.
at Autodesk.AutoCAD.EditorInput.Editor.CommandResult.Callback()

 

I get no error when I add "await" to the front

 

await ed.CommandAsync("Command","Parameter","Parameter")

 

But this makes the rest of the code not run until the command has finished, which isn't what I need.  What am I missing here?  Any help would be appreciated.  Thanks in advance!

0 Likes
Accepted solutions (1)
3,430 Views
5 Replies
Replies (5)
Message 2 of 6

_gile
Consultant
Consultant

Hi,

 

If you don't use the 'await' keyword, you have to explicitly handle CommandResult.OnCompleted, even if the callback does nothing:

 

Editor.CommandResult cmdResult = ed.CommandAsync("Command", "Parameter", "Parameter");
cmdResult.OnCompleted(() => { });


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 6

Anonymous
Not applicable

Thanks for the reply, Gilles!

 

I am using VB.Net.  What would be an equivallent to "() >= {}"?  Sorry if this seems basic, I am a rookie here trying to self teach.

 

Edit/Update:

 

I tried making a 'do nothing' function:

 

Public Function DoNothing()

Return nothing

End Function

 

cmdResult.OnCompleted(DoNothing())

 

I still get the same exception as above.

 

0 Likes
Message 4 of 6

_gile
Consultant
Consultant
Accepted solution

This is a lambda expression.

CommandResult.OnCompleted requires a delegate as parameter (System.Action) IOW, a Sub with no arguments.

This callback can't be null or Nothing (see the error message you got), so if you don't want to do anything after the command completed, you havr to pass a delegate which does nothing to OnCompleted (i.e. an empty Sub)

The VB equivalent should be:

 

Dim cmdResult As Editor.CommandResult = ed.CommandAsync("Command", "Parameter", "Parameter")
cmdResult.OnCompleted(Sub()
                      End Sub)

If you're not comfortable with lambda expressions, you can also write it this way:

 

Dim continuation As Action = Sub()
                                 'add some code here if you want
                             End Sub
Dim cmdResult As Editor.CommandResult = ed.CommandAsync("Command", "Parameter", "Parameter")
cmdResult.OnCompleted(continuation)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 6

_gile
Consultant
Consultant

If you prefer use a separate Sub as delegate:

 

Private Sub Continuation()
    'add some code here if you want
End Sub

To call this sub from the main command:

 

Dim cmdResult As Editor.CommandResult = ed.CommandAsync("Command", "Parameter", "Parameter")
cmdResult.OnCompleted(AddressOf Continuation)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 6

Anonymous
Not applicable

Thank you very much!  That got everything working and shed some light on some other questions I was having.

0 Likes