Wait for SendStringToExecute to complete

Wait for SendStringToExecute to complete

Anonymous
Not applicable
10,987 Views
35 Replies
Message 1 of 36

Wait for SendStringToExecute to complete

Anonymous
Not applicable
I figured out a method to wait for the SendStringToExecute to complete. It's similar to using a WebService, i send my command and subscribe to an event and complete my function when the command completes.



What i did was make a command that raises an event so i can just append that command onto any of my SendStringToExecute's

{color:#0000ff}

Public Event{color} eCommandComplete {color:#0000ff}As{color} EventHandler

<CommandMethod({color:#ff0000}"CommandComplete"{color})> _

{color:#0000ff}Sub{color} CommandComplete()

{color:#0000ff}RaiseEvent{color} eCommandComplete({color:#0000ff}Nothing{color}, {color:#0000ff}Nothing{color})

{color:#0000ff}End Sub



{color}Then my other command goes like



<CommandMethod({color:#ff0000}"SomeCommand"{color}> _

{color:#0000ff}Sub{color} SomeCommand()

{color:#339966}'Do Some Work

{color}{color:#0000ff}AddHandler{color} eCommandComplete, {color:#0000ff}AddressOf{color} SomeCommand

SendStringToExecute({color:#ff0000}"Zoom E CommandComplete "{color}, {color:#0000ff}False{color}, {color:#0000ff}False{color}, {color:#0000ff}False{color})

{color:#000000}{color:#0000ff}End Sub

{color}{color}{color:#0000ff}

Sub{color} SomeCommand({color:#0000ff}ByVal{color} sender {color:#0000ff}As Object{color}, {color:#0000ff}ByVal{color} e {color:#0000ff}As EventArgs{color})

{color:#339966}'Do Some other Work

{color}{color:#0000ff}End Sub{color}
0 Likes
10,988 Views
35 Replies
Replies (35)
Message 2 of 36

chiefbraincloud
Collaborator
Collaborator


While I avoid using SendStringToExecute as much as humanly possible (and then some), this is a cleverly simple solution to the asynchronous problem. Thanks for posting it.

Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 3 of 36

Anonymous
Not applicable
This seems to be exactly what I need, but I am having a little trouble understanding it. (I am using VB.net and am fairly new)

Any chance you could break down the basics of this?

I am using "SendStringToExecute" to use the ACAD erase command based on polyline points. Immediately after teh erase command I am searching the drawing for entities and it still finds the ones that should have been erased.

Any help?
0 Likes
Message 4 of 36

chiefbraincloud
Collaborator
Collaborator
When you say 'searching the drawing', I assume you mean you are iterating through the model and/or paper space objects correct?
If so, the 'erased' entities will still show up, but they have an IsErased property which should be true.
Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 5 of 36

Anonymous
Not applicable
Yes, by search I mean iterate the drawing DB.

I am checking the IsErased property but it returns true for objects that would have been erased by the SendStringToExecute command. This leads me to believe the code is getting ahead of the SendStringToExecute part.

Make sesne?
0 Likes
Message 6 of 36

Anonymous
Not applicable
Basically the Event Handler sounded like what I may need to prevent the code from jumping ahead.

I was wondering if someone could explain how the Even Handler logic works for this.
0 Likes
Message 7 of 36

Anonymous
Not applicable
Basically the Event Handler sounded like what I may need to prevent the code from jumping ahead.

I was wondering if someone could explain how the Even Handler logic works for this.
0 Likes
Message 8 of 36

chiefbraincloud
Collaborator
Collaborator
Basically, this part goes into your code exactly as is. It declares an event, and a command which will raise the event.

Public Event eCommandComplete As EventHandler
_
Sub CommandComplete()
RaiseEvent eCommandComplete(Nothing, Nothing)
End Sub

Then your first command will do whatever it does to collect points for the window or whatever, set the event handler for the eCommandComplete event to point to your CommandTwo, set up your command string to send to the command line, and notice that the last part of the command string calls the CommandComplete function you just created.


_
Sub SomeCommand()

'get your polyline points or whatever you are doing.

AddHandler eCommandComplete, AddressOf CommandTwo 'set the event handler to CommandTwo
SendStringToExecute("Zoom E CommandComplete ", False, False, False) 'Note the call to CommandComplete at the end.
End Sub

Sub CommandTwo(ByVal sender As Object, ByVal e As EventArgs)
'Iterate the DB and do your post process
End Sub

So the user runs CommandOne, which does some stuff and then calls SendStringToExecute, then ends.
the SendStringToExecute executes. Performing your erase, then calling CommandComplete.
CommandComplete raises your defined event, which calls whatever sub you have assigned the handler to with the AddHandler method.

Technically, you could eliminate the whole event setup and just create two commands, with the sendstring containing the call to the second command directly.

If that wasn't helpful, maybe you should post the pertinent bits of your code and someone can help figure out whats wrong.
Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 9 of 36

Anonymous
Not applicable
Ok, I'll will review this info and give it a shot tonight.

Thanks for the help, I will re-post once I give this a shot.
0 Likes
Message 10 of 36

Anonymous
Not applicable
Your only mistake here was believing what chief wrote about this being a
good solution to a problem. It certainly is not.

First, is there a reason why you are invoking the ERASE command rather than
just erasing objects by calling their Erase() method ?

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");


wrote in message news:6206202@discussion.autodesk.com...
Basically the Event Handler sounded like what I may need to prevent the code
from jumping ahead. I was wondering if someone could explain how the Even
Handler logic works for this.
0 Likes
Message 11 of 36

Anonymous
Not applicable
I do realize using the SendStringToExecute is not ideal, I am using it as a shortcut for now and hope to eventually do it the "right" way. I am also using SendStringToExecute with the Stretch and Copy commands for now. (Bascially trying to get the big picture of my project down before spending to much time on these things)

Either way, would be nice to see if this Event Handler method could work with my current situation.
0 Likes
Message 12 of 36

Anonymous
Not applicable
While it's true that using the managed ObjectARX API does
require some learning, you are still able to use the same
ActiveX API you use in VBA, which means that you can do
the same things you do in VBA without much more difficulty.

Hence, what you call a 'shortcut' is really not that, because
you could have easily wrote ActiveX code to delete entities
in far less time than you've wasted on this very bad kludge,
that will only box you into a corner down the road.

Stretching is another matter, and it's not doable via ActiveX,
and is not easily scriptable via the command line because of
its dependence on the current view.

Well, if you insist on following the chief's bad advice, then
hopefully, you're doing this on your own dime, rather than
on company time. I don't think chief is going to compensate
you for losses that result from following his bad advice.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");


wrote in message news:6206293@discussion.autodesk.com...
I do realize using the SendStringToExecute is not ideal, I am using it as a
shortcut for now and hope to eventually do it the "right" way. I am also
using SendStringToExecute with the Stretch and Copy commands for now.
(Bascially trying to get the big picture of my project down before spending
to much time on these things) Either way, would be nice to see if this Event
Handler method could work with my current situation.
0 Likes
Message 13 of 36

Anonymous
Not applicable
Ok, I am pretty sure I understand how the EventHandler works, but not sure it will work for my situation as I need to pass an arraylist to the second command(not sure how you would do that...)

What do you think of this, kind of crazy but might work...

I will still have a "CommandComplete" command that is appeneded on to the SendStringToExecute, but have this command do something I can look for. Directly after my SendString command I will go into a loop and look for whatever "CommandComplete" does and proceed once I recognize it. (Probably just have it create a tmp file on disk)

What do you think?
0 Likes
Message 14 of 36

Anonymous
Not applicable
Are you saying the original solution posted by timbot is not a good one and if so can you explain why?
0 Likes
Message 15 of 36

Anonymous
Not applicable
Ok, so I realize now that my SendString commands are not executed untill all my code is finished...

Hmm...
0 Likes
Message 16 of 36

chiefbraincloud
Collaborator
Collaborator
If the plan is to have one function like this:

Sub Whatever
'do stuff
SendStringToExecute()
Do
'nothing
Loop Until Commandcomplete
'do more stuff
End Sub

Then I think the SendStringToExecute will still be waiting to run. Essentially, your code must end, and return control back to the AutoCAD app, before the SendStringToExecute will fire.
Depending on what your arraylist contains, maybe you can temporarily store it in a dictionary for retrieval by the second command in the Event model.
Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 17 of 36

Anonymous
Not applicable
No, it's not a good solution, mainly because it attempts to solve
a problem that shouldn't exist in the first place, and only exists
as a result of not using the right tools for the job.

P/Invoking acedCommand() (as done by the CommandLine
class available from my website) eliminates the need to call
SendStringToExecute() to run AutoCAD commands, and with
that, eliminates the need to 'wait' for the commands to finish.

I've spoken at length about the problems associated with this
Autodesk-devised kludge, and you can read them here.

For example, what happens when a user presses enter to
repeat the last command *they* issued.

And then there's other issues like of undo grouping, and undoing
an entire command sequence as a whole; passing input that can't
be represented in string form (like objects) and so on.

Better yet, I'll just refer you to the assorted hacks/kludges that
you'll find on through-the-interface, which serve to demonstrate
what's wrong with automating AutoCAD by sending keystrokes
to the command line.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");


wrote in message news:6206412@discussion.autodesk.com...
Are you saying the original solution posted by timbot is not a good one and
if so can you explain why?
0 Likes
Message 18 of 36

Anonymous
Not applicable
Thanks Tony. I have been fortunate with .NET (unlike VBA) not to be in the situation of having to send commands to the commandline. If I do get into that situation I will look into your CommandLine class.
0 Likes
Message 19 of 36

Anonymous
Not applicable
wow, didn't realize people were responding to my post, must have forgot to have it notify me or something... anyhow, the main reazon i made this was because of my horrible troubles i was having with zooming, and i didn't want to use the interop because i had problems when we upgraded... this was just a suggestion for people learning like me to get thru a problem... yeah, the big problem was with repeating the command or undoing... but, all my zooms work great! B) i'll have to read your post about the P/Invoking acedCommand thing, i just wish there was a better zoom function built into the .net api, then all of my problems would be solved, untill then perhaps i'll learn some better way... thanks for the rebuke!
0 Likes
Message 20 of 36

Anonymous
Not applicable
Yep, thats bascially what I reliazed... (about the sendstring not executing untill later)

I would like to stay away from storing and jumping in and out of the code.

Do you know if there is any way to send to the command line and have it run in sync?
0 Likes