<?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: How to wait for an async method to complete inside a [LispFunction] ? in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/how-to-wait-for-an-async-method-to-complete-inside-a/m-p/13658235#M85173</link>
    <description>&lt;P&gt;I checkde your method and gives this error :&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;Severity	Code	Description	Project	File	Line	Suppression State
Error	CS1983	The return type of an async method must be void, Task, Task&amp;lt;T&amp;gt;, a task-like type, IAsyncEnumerable&amp;lt;T&amp;gt;, or IAsyncEnumerator&amp;lt;T&amp;gt;	&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;It seems &lt;FONT color="#0000FF"&gt;[LispFunction]&lt;/FONT&gt; doesn't support "async ResultBuffer".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just to clarify my previous setup:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;When I was using the &lt;FONT color="#0000FF"&gt;[extractOSMImages]&lt;/FONT&gt; method without &lt;FONT color="#0000FF"&gt;async/await&lt;/FONT&gt;, and the progress form was shown as modal, everything worked fine. The entire &lt;FONT color="#0000FF"&gt;[LispFunction]&lt;/FONT&gt; completed before returning control to the calling LISP code, and there were no issues.&lt;/LI&gt;&lt;LI&gt;&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;I switched the progress form to modeless because in modal mode, the progress bar was flickering and not updating smoothly.&lt;/LI&gt;&lt;LI&gt;&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;I also changed the download logic to be &lt;FONT color="#0000FF"&gt;async&lt;/FONT&gt; so that the &lt;FONT color="#0000FF"&gt;[HttpClient]&lt;/FONT&gt; requests could run asynchronously, which significantly improves download speed.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now with both changes (modeless form and &lt;FONT color="#0000FF"&gt;async&lt;/FONT&gt; download), the &lt;FONT color="#0000FF"&gt;[LispFunction]&lt;/FONT&gt;&amp;nbsp;returns immediately, before the download finishes or the form closes.&lt;/P&gt;</description>
    <pubDate>Fri, 30 May 2025 18:04:30 GMT</pubDate>
    <dc:creator>bahman.jf.68</dc:creator>
    <dc:date>2025-05-30T18:04:30Z</dc:date>
    <item>
      <title>How to wait for an async method to complete inside a [LispFunction] ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-wait-for-an-async-method-to-complete-inside-a/m-p/13657536#M85168</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;I'm working on a .NET plugin for AutoCAD (2017+) and I'm facing an issue when calling an &lt;FONT color="#0000FF"&gt;async&lt;/FONT&gt; method ((&lt;FONT color="#0000FF"&gt;extractOSMImagesAsync&lt;/FONT&gt;)) from a [&lt;FONT color="#0000FF"&gt;LispFunction&lt;/FONT&gt;].&lt;/P&gt;&lt;P&gt;I have a function that downloads satellite map tiles using &lt;FONT color="#0000FF"&gt;HttpClient&lt;/FONT&gt; and displays a Windows Form circular progress bar (Modeless form) during the download. The actual download logic &amp;nbsp;is asynchronous (&lt;FONT color="#0000FF"&gt;async&lt;/FONT&gt; Task). When I call this logic from a [&lt;FONT color="#0000FF"&gt;LispFunction&lt;/FONT&gt;], AutoCAD continues executing the rest of the code before the download finishes and before the progress form is closed.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;[LispFunction("RunOSMMaps")]
public ResultBuffer RunOSMMaps(ResultBuffer resBuf)
{
    Point3d vpll = ...; // parsed from resBuf
    Point3d vpur = ...;
    
    // Other parameters like image path, zoom, etc.

    ExtractImages runmaps = new ExtractImages();

    if (CheckNet())
    {
        // This is an async Task method
        runmaps.extractOSMImagesAsync(vpll, vpur, ...); // &amp;lt;-- returns immediately
    }

    // This message box is shown immediately before download completes
    Application.ShowAlertDialog(" Download completed!");

    return null;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;While in this Command Method it works correct, it means "Alert dialog" shows after complete downloading &lt;SPAN&gt;process : (It should be done by &lt;FONT color="#0000FF"&gt;LispFunction&lt;/FONT&gt;)&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;[CommandMethod("RunOSMMaps")]
        public async void RunOSMMapsCommand()
        {
            Point3d vpll = ...; // parsed from resBuf
            Point3d vpur = ...;

            if (CheckNet())
            {
                // This is an async Task method
                await runmaps.extractOSMImagesAsync(vpll, vpur...);
                );
            }

            Application.ShowAlertDialog(" Download completed!");
        }&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;Any suggestions or examples are appreciated!&lt;/P&gt;&lt;P&gt;Thanks in advance,&lt;/P&gt;</description>
      <pubDate>Fri, 30 May 2025 11:04:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-wait-for-an-async-method-to-complete-inside-a/m-p/13657536#M85168</guid>
      <dc:creator>bahman.jf.68</dc:creator>
      <dc:date>2025-05-30T11:04:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to wait for an async method to complete inside a [LispFunction] ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-wait-for-an-async-method-to-complete-inside-a/m-p/13657900#M85169</link>
      <description>&lt;P&gt;Your CommandMethod works as you expect because it is marked as &lt;EM&gt;&lt;STRONG&gt;async&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;and it uses &lt;STRONG&gt;&lt;EM&gt;await&lt;/EM&gt;&lt;/STRONG&gt;&amp;nbsp; which is what causes the compiler to rewrite your code so that whatever follows the await-ed method call does not run until the await-ed method has returned.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Adding the same async and await keywords to the LispFunction should make it work like the CommandMethod::&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;[LispFunction("RunOSMMaps")]
public async ResultBuffer RunOSMMaps(ResultBuffer resBuf)
{
    Point3d vpll = ...; // parsed from resBuf
    Point3d vpur = ...;
    
    // Other parameters like image path, zoom, etc.

    ExtractImages runmaps = new ExtractImages();

    if (CheckNet())
    {
        // This is an async Task method
        await runmaps.extractOSMImagesAsync(vpll, vpur, ...); 
    }

    // This message box is shown immediately before download completes
    Application.ShowAlertDialog(" Download completed!");

    return null;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The only difference is that the method is marked as &lt;STRONG&gt;async&lt;/STRONG&gt; and the call to extractOSMImagesAsync() is preceded by &lt;STRONG&gt;await&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, if you are expecting the calling LISP code to wait until&amp;nbsp;extractOSMImagesAsync() has returned, that is not going to happen. The LispFunction will return to its caller before&amp;nbsp;extractOSMImagesAsync() has returned, and the only way to prevent that is by not using async/await.&lt;/P&gt;</description>
      <pubDate>Fri, 30 May 2025 15:46:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-wait-for-an-async-method-to-complete-inside-a/m-p/13657900#M85169</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2025-05-30T15:46:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to wait for an async method to complete inside a [LispFunction] ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-wait-for-an-async-method-to-complete-inside-a/m-p/13657911#M85170</link>
      <description>&lt;P&gt;What happens if you silently invoke the CommandMethod from your LispFunction?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 May 2025 13:48:54 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-wait-for-an-async-method-to-complete-inside-a/m-p/13657911#M85170</guid>
      <dc:creator>BlackBox_</dc:creator>
      <dc:date>2025-05-30T13:48:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to wait for an async method to complete inside a [LispFunction] ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-wait-for-an-async-method-to-complete-inside-a/m-p/13658235#M85173</link>
      <description>&lt;P&gt;I checkde your method and gives this error :&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;Severity	Code	Description	Project	File	Line	Suppression State
Error	CS1983	The return type of an async method must be void, Task, Task&amp;lt;T&amp;gt;, a task-like type, IAsyncEnumerable&amp;lt;T&amp;gt;, or IAsyncEnumerator&amp;lt;T&amp;gt;	&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;It seems &lt;FONT color="#0000FF"&gt;[LispFunction]&lt;/FONT&gt; doesn't support "async ResultBuffer".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just to clarify my previous setup:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;When I was using the &lt;FONT color="#0000FF"&gt;[extractOSMImages]&lt;/FONT&gt; method without &lt;FONT color="#0000FF"&gt;async/await&lt;/FONT&gt;, and the progress form was shown as modal, everything worked fine. The entire &lt;FONT color="#0000FF"&gt;[LispFunction]&lt;/FONT&gt; completed before returning control to the calling LISP code, and there were no issues.&lt;/LI&gt;&lt;LI&gt;&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;I switched the progress form to modeless because in modal mode, the progress bar was flickering and not updating smoothly.&lt;/LI&gt;&lt;LI&gt;&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;I also changed the download logic to be &lt;FONT color="#0000FF"&gt;async&lt;/FONT&gt; so that the &lt;FONT color="#0000FF"&gt;[HttpClient]&lt;/FONT&gt; requests could run asynchronously, which significantly improves download speed.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now with both changes (modeless form and &lt;FONT color="#0000FF"&gt;async&lt;/FONT&gt; download), the &lt;FONT color="#0000FF"&gt;[LispFunction]&lt;/FONT&gt;&amp;nbsp;returns immediately, before the download finishes or the form closes.&lt;/P&gt;</description>
      <pubDate>Fri, 30 May 2025 18:04:30 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-wait-for-an-async-method-to-complete-inside-a/m-p/13658235#M85173</guid>
      <dc:creator>bahman.jf.68</dc:creator>
      <dc:date>2025-05-30T18:04:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to wait for an async method to complete inside a [LispFunction] ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-wait-for-an-async-method-to-complete-inside-a/m-p/13658242#M85174</link>
      <description>&lt;P&gt;I don't think invoking a &lt;FONT color="#0000FF"&gt;[CommandMethod]&lt;/FONT&gt; from within a &lt;FONT color="#0000FF"&gt;[LispFunction]&lt;/FONT&gt; would solve the issue. Even if the command is launched successfully, the &lt;FONT color="#0000FF"&gt;[LispFunction]&lt;/FONT&gt; itself will return immediately and continue executing the remaining LISP code — while the &lt;FONT color="#0000FF"&gt;[CommandMethod]&lt;/FONT&gt; is still running asynchronously. So we end up with the same problem: the LISP side doesn’t wait for the download or &lt;FONT color="#0000FF"&gt;async&lt;/FONT&gt; task to complete.&lt;/P&gt;</description>
      <pubDate>Fri, 30 May 2025 18:13:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-wait-for-an-async-method-to-complete-inside-a/m-p/13658242#M85174</guid>
      <dc:creator>bahman.jf.68</dc:creator>
      <dc:date>2025-05-30T18:13:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to wait for an async method to complete inside a [LispFunction] ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-wait-for-an-async-method-to-complete-inside-a/m-p/13658620#M85176</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/7889601"&gt;@bahman.jf.68&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;I checkde your method and gives this error :&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;LI-CODE lang="general"&gt;Severity	Code	Description	Project	File	Line	Suppression State
Error	CS1983	The return type of an async method must be void, Task, Task&amp;lt;T&amp;gt;, a task-like type, IAsyncEnumerable&amp;lt;T&amp;gt;, or IAsyncEnumerator&amp;lt;T&amp;gt;	&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;It seems &lt;FONT color="#0000FF"&gt;[LispFunction]&lt;/FONT&gt; doesn't support "async ResultBuffer".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A LispFunction can return void:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public async void RunOSMMaps(ResultBuffer resBuf)
{
   ...
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, based on your comments, I think you're expecting the calling LISP code to wait until the download operation is completed, which it will not. Even with async/await you cannot stop a LispFunction from returning before the await'ed method call returns. &lt;STRONG&gt;&lt;EM&gt;await&lt;/EM&gt;&lt;/STRONG&gt; only delays execution of the code in the async method that follows &lt;EM&gt;&lt;STRONG&gt;await&lt;/STRONG&gt;&lt;/EM&gt;, but the method itself will still return before the await'ed method call returns.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If there's progress dialog involved, things can be much more complicated.&lt;/P&gt;</description>
      <pubDate>Sat, 31 May 2025 01:04:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-wait-for-an-async-method-to-complete-inside-a/m-p/13658620#M85176</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2025-05-31T01:04:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to wait for an async method to complete inside a [LispFunction] ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-wait-for-an-async-method-to-complete-inside-a/m-p/13658884#M85179</link>
      <description>&lt;P&gt;somewhat I knew that &lt;FONT color="#0000FF"&gt;[LispFunction]&lt;/FONT&gt; runs as sync, actually I was looking for a way to stop running the rest of the Lisp code when calling&amp;nbsp;&lt;FONT color="#0000FF"&gt;[extractOSMImagesAsync] &lt;FONT color="#333333"&gt;and before completing it by another way.&lt;/FONT&gt;&lt;/FONT&gt;&lt;BR /&gt;Thanks again.&lt;/P&gt;</description>
      <pubDate>Sat, 31 May 2025 09:35:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-wait-for-an-async-method-to-complete-inside-a/m-p/13658884#M85179</guid>
      <dc:creator>bahman.jf.68</dc:creator>
      <dc:date>2025-05-31T09:35:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to wait for an async method to complete inside a [LispFunction] ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-wait-for-an-async-method-to-complete-inside-a/m-p/13659303#M85180</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/7889601"&gt;@bahman.jf.68&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;somewhat I knew that &lt;FONT color="#0000FF"&gt;[LispFunction]&lt;/FONT&gt; runs as sync, actually I was looking for a way to stop running the rest of the Lisp code when calling&amp;nbsp;&lt;FONT color="#0000FF"&gt;[extractOSMImagesAsync] &lt;FONT color="#333333"&gt;and before completing it by another way.&lt;/FONT&gt;&lt;/FONT&gt;&lt;BR /&gt;Thanks again.&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;You can prevent your LispFunction from returning until your asynchronous method call returns by just not using async/await. But in that case, you also have to deal with blocking the UI.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;See if you can do something with this:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;[LispFunction("RunOSMMaps")]
public ResultBuffer RunOSMMaps(ResultBuffer resBuf)
{
   Point3d vpll = ...; // parsed from resBuf
   Point3d vpur = ...;

   // Other parameters like image path, zoom, etc.

   ExtractImages runmaps = new ExtractImages();

   if (CheckNet())
   {
      var start = DateTime.Now;

      try   // &amp;lt;- Not optional - If an exception leaks from this method, it can crash AutoCAD
      {
         var task = runmaps.extractOSMImagesAsync(vpll, vpur, ...);
         
         /// Don't allow continuation to run on worker thread:
         task.ConfigureAwait(false);
         
         var elapsed = DateTime.Now - start;

         // Set a timeout that aborts the operation if
         // it takes longer than a specified duration
         // (30 seconds in this example):
         var timeout = TimeSpan.FromSeconds(30); 

         while(!task.IsCompleted)
         {
            elapsed = DateTime.Now - start;
            if(elapsed &amp;gt; timeout)
            {
               UpdateUI("\nError - operation timed out", 0);
               return null;
            }
            UpdateUI($"\rDownloading files {elapsed.TotalSeconds:F2}", 500);
         }

         task.GetAwaiter().GetResult();  // forces completion of the async operation
      }
      catch(System.Exception ex)
      {
         UpdateUI($"\nError: {ex.ToString()}");
         return null;
      }

      var duration = (DateTime.Now - start).Seconds;
      UpdateUI($"\nDownload Completed in {duration:F2} seconds.\n");

   }

   return null;
}

static void UpdateUI(string msg, int duration = 0)
{
   Application.DocumentManager.MdiActiveDocument?
      .Editor.WriteMessage(msg);

   if(duration &amp;gt; 0)
      Thread.Sleep(duration);
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 31 May 2025 19:23:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-wait-for-an-async-method-to-complete-inside-a/m-p/13659303#M85180</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2025-05-31T19:23:50Z</dc:date>
    </item>
  </channel>
</rss>

