Hello,
although I've seen many questions and answers on this subject none of them works for me except this one:
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\x03\x03", false, true, false);
but it runs asynchronously and that is not what I want.
Some answers that I found and tried out are more than 10 years old so I guess maybe they used to work on older version of CAD but now they don't.
So to be clear, I need a way to cancel any active command synchronously, using C# and I work in AutoCAD 2018.
I tried to use the Editor.Command() and Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;.ActiveDocument.SendCommand(), but i don't know what string to pass. Tried so many without success.
Please help, this is driving me crazy.
Just install an handler on Document.CommandCancelled event before calling SendStringToExecute(). This way, you'll be able to run your code after the command is cancelled.
Interesting suggestion. Thank you very much, but I must say that I'm still hoping to get an answer on how to use Editor.Command(). If no one replays with that I will certainly use your suggestion.
Just to be clear, are you looking to cancel the active command, regardless of what it is? Or to run something when the active command is canceled by whatever method?
This is the situation:
In my app, i have a Palette Set with Tool Strip. When user clicks on a button on a Tool Strip a new command is activated. But the problem is that if there is already a command running it won't be canceled. So when a button is pressed in my Pallete Set i need to check if there are any active commands and if so cancel them, and then start the new command.
Why are you not putting the command you want to launch after the ETX chars?
Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\x03\x03YOURCOMMAND\n", false, false, false);
And I think that in your case, you can pass false for wrapUpInactiveDoc as you are working on the active document.
Editor.Command() does not work in an applicationContext (I think it throws an invalid input exception).
When exactly does this event get called? Only if a command is cancelled before finishing or whenever a cancel happens?
I ask because I have this code and it never calls Document_CommandCancelled
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
doc.CommandCancelled += new CommandEventHandler(Document_CommandCancelled);
doc.SendStringToExecute("\x03\x03", false, true, false);
If this isn't the way to do it, how do I get my code to fire Document_CommandCancelled after doc.SendStringToExecute?
Can't find what you're looking for? Ask the community or share your knowledge.