Author Topic: How does Editor shows the message immediately?  (Read 6425 times)

0 Members and 1 Guest are viewing this topic.

xys1995

  • Mosquito
  • Posts: 17
How does Editor shows the message immediately?
« on: September 11, 2016, 11:56:23 PM »
When use the function Editor.WriteMessage() in a for loop,the message can't be displayed immediately.And all messages were listed  at the same time till the CAD command finished.
Like this:

for (int i = 0; i < tps.Count; i++)
{
       ......
      ed..WriteMessage("\nCalculating {0}...", tp.pointNo);
}

Does Editor  class has any method to show the message immediately?

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: How does Editor shows the message immediately?
« Reply #1 on: September 12, 2016, 12:55:44 AM »
are you doing this inside a transaction? If so nothing will happen until the transaction is committed.... just a thought.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.
- J.R.R. Tolkien

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2131
  • class keyThumper<T>:ILazy<T>
Re: How does Editor shows the message immediately?
« Reply #2 on: September 12, 2016, 12:56:22 AM »
I assume that should be  tps.pointNo


The message should be written as the loop is iterated.

What is tps ??
What does the rest on the loop look like  ?
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

xys1995

  • Mosquito
  • Posts: 17
Re: How does Editor shows the message immediately?
« Reply #3 on: September 12, 2016, 02:11:58 AM »
are you doing this inside a transaction? If so nothing will happen until the transaction is committed.... just a thought.

There is no transaction.
next code is of the same:
Code: [Select]
[CommandMethod("nn")]
        public static void nn()
        {
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            for (int i = 0; i < 10000; i++)
            {
                ed.WriteMessage("\n{0}", i);
            }
        }

The message can be shown until the for loop finished.
I think that I should see the message printed form 0 to 9999 one by one.But, after wait for about one second,I see all the messages were printed togather.
Maybe beacuse of the Single-thread?
« Last Edit: September 12, 2016, 02:27:50 AM by xys1995 »

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: How does Editor shows the message immediately?
« Reply #4 on: September 12, 2016, 02:24:28 AM »
try putting a 'sleep' in the loop to slow it down, that loop would finish quickly and it would seem like it was waiting to finish, especially if you just loaded the dll there could even be a bit if JIT lag to make it seem like it's waiting.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.
- J.R.R. Tolkien

xys1995

  • Mosquito
  • Posts: 17
Re: How does Editor shows the message immediately?
« Reply #5 on: September 12, 2016, 02:43:35 AM »
try putting a 'sleep' in the loop to slow it down, that loop would finish quickly and it would seem like it was waiting to finish, especially if you just loaded the dll there could even be a bit if JIT lag to make it seem like it's waiting.

I tried.But it seems that does'nt work....
Code: [Select]
[CommandMethod("nn")]
        public static void nn()
        {
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            for (int i = 0; i < 10000; i++)
            {
                ed.WriteMessage("\n{0}", i);
                Thread.Sleep(1000);
            }
        }

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: How does Editor shows the message immediately?
« Reply #6 on: September 12, 2016, 05:28:15 PM »
hmmm, it works on my machine, and I tried a recursion as well to show another option.

Sometimes VS doesn't recompile when you run a debug, make sure you do a full build to ensure you are using the latest version of your code.

My testing is done in Bricscad so maybe there is something there, I've come across plenty of weird anomalies between the 2 before.

Code - C#: [Select]
  1.         public static void recurtest(int count)
  2.         {
  3.             Editor ed = _AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
  4.             if (count <= 10)
  5.             {
  6.                 ed.WriteMessage("\n{0}", count);
  7.                 System.Threading.Thread.Sleep(1000);
  8.                 recurtest(count + 1);
  9.             }
  10.         }
  11.  
  12.         [CommandMethod("nn")]
  13.         public static void test()
  14.         {
  15.             recurtest(1);
  16.         }
  17.  
  18.         [CommandMethod("nnn")]
  19.         public static void test2()
  20.         {
  21.             Editor ed = _AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
  22.             for (int i = 1; i <= 10; i++)
  23.             {
  24.                 ed.WriteMessage("\n{0}", i);
  25.                 System.Threading.Thread.Sleep(1000);
  26.             }
  27.         }
  28.  
« Last Edit: September 12, 2016, 05:40:18 PM by MickD »
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.
- J.R.R. Tolkien

xys1995

  • Mosquito
  • Posts: 17
Re: How does Editor shows the message immediately?
« Reply #7 on: September 12, 2016, 09:54:54 PM »
Recompile and used recursion method,still can't work in AutoCAD2016.
But in AutoCAD2008,it works very well.
MickD,thank you for your help.

Jeff H

  • Needs a day job
  • Posts: 6149
Re: How does Editor shows the message immediately?
« Reply #8 on: September 12, 2016, 10:19:41 PM »
I just tested on 2016 and seems like it is doing the same thing.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2131
  • class keyThumper<T>:ILazy<T>
Re: How does Editor shows the message immediately?
« Reply #9 on: September 12, 2016, 10:49:03 PM »
I just tested on 2016 and seems like it is doing the same thing.

The same thing as .... ?
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: How does Editor shows the message immediately?
« Reply #10 on: September 12, 2016, 11:01:13 PM »
Recompile and used recursion method,still can't work in AutoCAD2016.
But in AutoCAD2008,it works very well.
MickD,thank you for your help.

Me thinks maybe AutoCAD is wrapping the Command in some kind of Transaction? Something to do with removing Fibres??? Only guessing now....
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.
- J.R.R. Tolkien

Jeff H

  • Needs a day job
  • Posts: 6149
Re: How does Editor shows the message immediately?
« Reply #11 on: September 13, 2016, 12:02:57 AM »
I just tested on 2016 and seems like it is doing the same thing.

The same thing as .... ?
Sorry I was seeing same behavior as xys1995,  it would print it at one time to the command line.

You can always use the evil :evil:  DoEvents
Code - C#: [Select]
  1.         [CommandMethod("nn")]
  2.         public static void nn()
  3.         {
  4.             Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  5.             for (int i = 0; i < 10; i++)
  6.             {
  7.                 ed.WriteMessage("\n{0}", i);
  8.                 Thread.Sleep(500);
  9.                 System.Windows.Forms.Application.DoEvents();
  10.             }
  11.         }
  12.  

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: How does Editor shows the message immediately?
« Reply #12 on: September 13, 2016, 07:17:00 AM »
Remember your working on the UI thread when working with AutoCAD. So until there is a pause in your code, i.e. user input, DoEvents(). Your not going to see any command line updates.  It looks like your trying to show the user some kind of update. Might I suggest ProgressMeter().  It's not perfect but AutoCAD does a decent job of updating it between operations.
Revit 2019, AMEP 2019 64bit Win 10

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: How does Editor shows the message immediately?
« Reply #13 on: September 13, 2016, 10:27:25 AM »
Recompile and used recursion method,still can't work in AutoCAD2016.
But in AutoCAD2008,it works very well.
MickD,thank you for your help.

Me thinks maybe AutoCAD is wrapping the Command in some kind of Transaction? Something to do with removing Fibres??? Only guessing now....

I suspect it's a side effect of changes to the command line palette to support click-able command options and other new features.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

xys1995

  • Mosquito
  • Posts: 17
Re: How does Editor shows the message immediately?
« Reply #14 on: September 13, 2016, 10:09:28 PM »
Remember your working on the UI thread when working with AutoCAD. So until there is a pause in your code, i.e. user input, DoEvents(). Your not going to see any command line updates.  It looks like your trying to show the user some kind of update. Might I suggest ProgressMeter().  It's not perfect but AutoCAD does a decent job of updating it between operations.

Very interesting。
Code: [Select]
       [CommandMethod("nn")]
        public static void nn()
        {
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

            int totalTimes = 10;
            ProgressMeter pm = new ProgressMeter();
            pm.SetLimit(totalTimes);
            pm.Start();

            for (int i = 0; i < totalTimes; i++)
            {
                pm.MeterProgress();
                Thread.Sleep(1000);
                //ed.WriteMessage("\n{0}", i);
                //System.Windows.Forms.Application.DoEvents();
            }
            pm.Stop();
        }