Command Window does not get updated when using WriteMessage() from a long command

By Adam Nagy

I'm using WriteMessage() in a longer command and would like to keep the user updated by writing messages to the command window. Unfortunately, when I use WriteMessage(), its content only appears once my command finished. How could I update/refresh the Command Window to show the text I wrote?

Solution

If you add a carriage return (C# n, VB.NET vbCr/vbCrLf) to the end of your text then it will appear straight away. As mentioned in the comments section, VB.NET vbCr might not work as well as vbCrLf does.

[CommandMethod("AEN1WriteInfo")]
static public void AEN1WriteInfo()
{
  Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.
      DocumentManager.MdiActiveDocument.Editor;
  ed.WriteMessage("Somethingn");
  for (int i = 0; i < 3; i++)
  {
    System.Threading.Thread.Sleep(1000);
    // carriage return at the end
    ed.WriteMessage(i.ToString() + "n"); 
  }
}

Comments

4 responses to “Command Window does not get updated when using WriteMessage() from a long command”

  1. I had the same problem. For me the solution was to add
    “System.Windows.Forms.Application.DoEvents()” inside the “For Loop”.

  2. Michael Leslie Avatar
    Michael Leslie

    This is a useful trick, but I have found using AutoCAD 2013 and VB 2012 that if I pass vbCr my message does not display whereas with vbCrLf it does.
    Is this new to this version? I notice in most sample code, they use “/n” or vbCr, so it works for them . . .
    You may want to change the VB.NET note at the end of your example. Thanks.

  3. Don’t work for me. I need to call DoEvents to force the refresh like @Rolando

  4. I would like to advise you to keep improving and keep doing what you’re doing.

Leave a Reply to Michael LeslieCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading