<?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 Re : AcadDocument.SendCommand doesn't wait (executes asynchronously) in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/acaddocument-sendcommand-doesn-t-wait-executes-asynchronously/m-p/5475557#M41672</link>
    <description>&lt;P&gt;Hi FRFR1426,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That could be the cause. &amp;nbsp;However it is essential to be in session mode since the window that appears is responsible for allowing the user to automatically modify sets of drawings.&lt;/P&gt;</description>
    <pubDate>Tue, 20 Jan 2015 12:53:01 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2015-01-20T12:53:01Z</dc:date>
    <item>
      <title>AcadDocument.SendCommand doesn't wait (executes asynchronously)</title>
      <link>https://forums.autodesk.com/t5/net-forum/acaddocument-sendcommand-doesn-t-wait-executes-asynchronously/m-p/5472883#M41670</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'll try to keep this breif and to the point.&lt;/P&gt;&lt;P&gt;I have two C# assemblies used for starting my application:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The first creates an AutoCAD instance and execute commands using COM interop.&lt;/P&gt;&lt;P&gt;The second is a .NET dll which has a command entry point.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The application starts up fine, but I notice I am having a synchronization issue.&lt;/P&gt;&lt;P&gt;Apparently, when I call&amp;nbsp;&lt;STRONG&gt;AcadDocument.SendCommand( )&lt;/STRONG&gt;&amp;nbsp;the function doesn't wait for my second assemply to finish.&lt;/P&gt;&lt;P&gt;In the second assemby, I am using WPF (Windows Presentation Foundation) in order to display a GUI, which is shown via&amp;nbsp;&lt;STRONG&gt;Window.ShowDialog( )&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My expectation is that &lt;STRONG&gt;Window.ShowDialog( )&lt;/STRONG&gt; actually causes the first assembly to not finish the&amp;nbsp;&lt;STRONG&gt;SendCommand&lt;/STRONG&gt;&lt;STRONG&gt;( )&lt;/STRONG&gt; function. &amp;nbsp;However, I am apparently wrong.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyways... let me get some code up here so you can visualize what is happening.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Assembly 1:&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;//Create AutoCAD instance, then...&lt;BR /&gt;&lt;BR /&gt;acadApp.ActiveDocument.SendCommand("(command \"NETLOAD\""+@"""C:\\acad\\networkdll\\SecondAssembly.dll"") ");
acadApp.ActiveDocument.SendCommand("#MYCOMMAND 0 ");&lt;BR /&gt;&lt;BR /&gt;//Close the startup drawing (this requires waiting &lt;SPAN class="lia-mentions-autocomplete"&gt;&lt;SPAN class="lia-mentions-trigger"&gt;@ SendCommand) because&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;//Drawing will cause a COMException otherwise.  'Drawing is busy'&lt;BR /&gt;//Mostly likely since the ActiceDocument is the startup drawing.&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Assembly 2:&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;[CommandMethod("#MYCOMMAND", CommandFlags.Session)]
public void CommandEntry()
{   &lt;BR /&gt;    //Process command argument, then...&lt;BR /&gt;&lt;BR /&gt;    //Create window.&lt;BR /&gt;    MainWindow mainWin = new MainWindow();&lt;BR /&gt;    mainWin.ShowDialog();
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Instead of the applicating waiting for&amp;nbsp;&lt;STRONG&gt;SendCommand( )&lt;/STRONG&gt; to finish, it ends up hanging at the entry of a foreach loop (makes absolutely 0 sense to me). &amp;nbsp;The loop is used for interating through the document list in order to find the startup drawing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Well, am I stuck? &amp;nbsp;Does anyone know if there is a way to synchronize this in a better way?&lt;/P&gt;&lt;P&gt;As a desparation, I could create a lock file I suppose in order to determine when to continue processing; that is a workaround however.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyways, I greatly appreciate those of you who spend your time helping an random person over the internet.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks and God bless!&lt;/P&gt;&lt;P&gt;Nicholas Miller&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jan 2015 21:42:19 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/acaddocument-sendcommand-doesn-t-wait-executes-asynchronously/m-p/5472883#M41670</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2015-01-16T21:42:19Z</dc:date>
    </item>
    <item>
      <title>Re : AcadDocument.SendCommand doesn't wait (executes asynchronously)</title>
      <link>https://forums.autodesk.com/t5/net-forum/acaddocument-sendcommand-doesn-t-wait-executes-asynchronously/m-p/5473133#M41671</link>
      <description>&lt;P&gt;I think it's because you are using the Session flag on your&amp;nbsp;#MYCOMMAND command. You're then in application context and SendCommand can not run synchronously.&lt;/P&gt;</description>
      <pubDate>Sat, 17 Jan 2015 09:49:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/acaddocument-sendcommand-doesn-t-wait-executes-asynchronously/m-p/5473133#M41671</guid>
      <dc:creator>FRFR1426</dc:creator>
      <dc:date>2015-01-17T09:49:53Z</dc:date>
    </item>
    <item>
      <title>Re : AcadDocument.SendCommand doesn't wait (executes asynchronously)</title>
      <link>https://forums.autodesk.com/t5/net-forum/acaddocument-sendcommand-doesn-t-wait-executes-asynchronously/m-p/5475557#M41672</link>
      <description>&lt;P&gt;Hi FRFR1426,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That could be the cause. &amp;nbsp;However it is essential to be in session mode since the window that appears is responsible for allowing the user to automatically modify sets of drawings.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jan 2015 12:53:01 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/acaddocument-sendcommand-doesn-t-wait-executes-asynchronously/m-p/5475557#M41672</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2015-01-20T12:53:01Z</dc:date>
    </item>
    <item>
      <title>Re : AcadDocument.SendCommand doesn't wait (executes asynchronously)</title>
      <link>https://forums.autodesk.com/t5/net-forum/acaddocument-sendcommand-doesn-t-wait-executes-asynchronously/m-p/5475590#M41673</link>
      <description>&lt;P&gt;You can add an event handler to the Document.EndCommand event to wait for the end of NETLOAD execution.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jan 2015 13:10:37 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/acaddocument-sendcommand-doesn-t-wait-executes-asynchronously/m-p/5475590#M41673</guid>
      <dc:creator>FRFR1426</dc:creator>
      <dc:date>2015-01-20T13:10:37Z</dc:date>
    </item>
    <item>
      <title>Re : AcadDocument.SendCommand doesn't wait (executes asynchronously)</title>
      <link>https://forums.autodesk.com/t5/net-forum/acaddocument-sendcommand-doesn-t-wait-executes-asynchronously/m-p/5475622#M41674</link>
      <description>&lt;P&gt;Hm, I'm trying to see how that solves this. The NETLOAD is required for loading a .DLL, while the #MYCOMMAND executes a function in the .DLL.&lt;/P&gt;&lt;P&gt;My understanding is that NETLOAD is fine. I need to perform a blocking wait after I execute #MYCOMMAND, since Session Mode causes it to be asynchronous.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;On an interesting side note, this adndevblog says it should be synchronous the way I'm attributing the command function:&lt;BR /&gt;&lt;A href="http://adndevblog.typepad.com/autocad/2012/05/why-does-sendcommand-run-asynchronously.html" target="_self"&gt;http://adndevblog.typepad.com/autocad/2012/05/why-does-sendcommand-run-asynchronously.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using AutoCAD 2012 if that affects anything.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jan 2015 13:27:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/acaddocument-sendcommand-doesn-t-wait-executes-asynchronously/m-p/5475622#M41674</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2015-01-20T13:27:58Z</dc:date>
    </item>
    <item>
      <title>Re : AcadDocument.SendCommand doesn't wait (executes asynchronously)</title>
      <link>https://forums.autodesk.com/t5/net-forum/acaddocument-sendcommand-doesn-t-wait-executes-asynchronously/m-p/5475683#M41675</link>
      <description>&lt;P&gt;If you are in the application context (with session flag on), SendCommand should execute&amp;nbsp;&lt;SPAN&gt;synchronously. But may be it will not be synchronous if you're calling for user interaction (Editor.Getxxx methods or ShowDialog...) in your command. Just use CommandEnded event and wait for the end of your #MYCOMMAND command.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jan 2015 14:06:26 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/acaddocument-sendcommand-doesn-t-wait-executes-asynchronously/m-p/5475683#M41675</guid>
      <dc:creator>FRFR1426</dc:creator>
      <dc:date>2015-01-20T14:06:26Z</dc:date>
    </item>
    <item>
      <title>Re : AcadDocument.SendCommand doesn't wait (executes asynchronously)</title>
      <link>https://forums.autodesk.com/t5/net-forum/acaddocument-sendcommand-doesn-t-wait-executes-asynchronously/m-p/5475797#M41676</link>
      <description>&lt;P&gt;Ha! It worked!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks much! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using &lt;A href="http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle%28v=vs.110%29.aspx" target="_self"&gt;System.Threading.EventWaitHandle&lt;/A&gt;&amp;nbsp;for blocking.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Here is what the new code looks like:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;//Create AutoCAD instance, then...

acadApp.ActiveDocument.SendCommand("(command \"NETLOAD\""+@"""C:\\acad\\networkdll\\SecondAssembly.dll"") ");
acadApp.ActiveDocument.SendCommand("#MYCOMMAND 0 ");
&lt;BR /&gt;&lt;STRONG&gt;//Register EndCommand handler.
_DAcadApplicationEvents_EndCommandEventHandler handler = new  &lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;    _DAcadApplicationEvents_EndCommandEventHandler(CommandEnded);&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;BR /&gt; acadApp.EndCommand += handler;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt; waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt; waitHandle.WaitOne();&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt; acadApp.EndCommand -= handler;&lt;/STRONG&gt;

//Close the startup drawing (this requires waiting @ SendCommand) because
//Drawing will cause a COMException otherwise.  'Drawing is busy'
//Mostly likely since the ActiceDocument is the startup drawing.&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;Event Handler:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;public void CommandEnded(string globalCommandName)
{
    System.Windows.MessageBox.Show(globalCommandName + " just ended.");
    waitHandle.Set();
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The second assembly was left unchanged.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;HR /&gt;&lt;P&gt;While tinkering, I confirmed that using 'session lock files' to perform blocking works.&lt;/P&gt;&lt;P&gt;In&amp;nbsp;&lt;STRONG&gt;Assembly 1&lt;/STRONG&gt; create a lock file, then wait as long as the file exists.&lt;/P&gt;&lt;P&gt;In&amp;nbsp;&lt;STRONG&gt;Assembly 2&lt;/STRONG&gt;&amp;nbsp;delete the file when required.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jan 2015 15:33:32 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/acaddocument-sendcommand-doesn-t-wait-executes-asynchronously/m-p/5475797#M41676</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2015-01-20T15:33:32Z</dc:date>
    </item>
  </channel>
</rss>

