TheSwamp

Code Red => .NET => Topic started by: xys1995 on September 11, 2016, 11:56:23 PM

Title: How does Editor shows the message immediately?
Post by: xys1995 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?
Title: Re: How does Editor shows the message immediately?
Post by: MickD 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.
Title: Re: How does Editor shows the message immediately?
Post by: kdub_nz 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  ?
Title: Re: How does Editor shows the message immediately?
Post by: xys1995 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?
Title: Re: How does Editor shows the message immediately?
Post by: MickD 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.
Title: Re: How does Editor shows the message immediately?
Post by: xys1995 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);
            }
        }
Title: Re: How does Editor shows the message immediately?
Post by: MickD 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.  
Title: Re: How does Editor shows the message immediately?
Post by: xys1995 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.
Title: Re: How does Editor shows the message immediately?
Post by: Jeff H on September 12, 2016, 10:19:41 PM
I just tested on 2016 and seems like it is doing the same thing.
Title: Re: How does Editor shows the message immediately?
Post by: kdub_nz 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 .... ?
Title: Re: How does Editor shows the message immediately?
Post by: MickD 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....
Title: Re: How does Editor shows the message immediately?
Post by: Jeff H 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.  
Title: Re: How does Editor shows the message immediately?
Post by: MexicanCustard 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.
Title: Re: How does Editor shows the message immediately?
Post by: dgorsman 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.
Title: Re: How does Editor shows the message immediately?
Post by: xys1995 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();
        }
Title: Re: How does Editor shows the message immediately?
Post by: MickD on September 13, 2016, 11:06:59 PM
What happens if you uncomment the ed.WriteMessage?

On Bricscad it works as expected, i.e. it writes to command prompt as well as increasing the progress meter.
Title: Re: How does Editor shows the message immediately?
Post by: xys1995 on September 13, 2016, 11:24:10 PM
Can't be listed one by one.
Title: Re: How does Editor shows the message immediately?
Post by: Chumplybum on September 15, 2016, 01:50:58 AM
I'm still unsure what you're trying to achieve... do you want the message to display the percentage complete in one line only, or display a new line for each percentage complete? Using environment.newline might be what you're after.

using this:
Code: [Select]
For counter As Integer = 1 To 100
            ed.WriteMessage("\n{0}", counter)
        Next

i get:
\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\...

using this:
Code: [Select]
For counter As Integer = 1 To 100
            ed.WriteMessage(String.Format("{0}count is {1}", Environment.NewLine, counter))
        Next

i get:
count is 1
count is 2
count is 3
count is 4
count is 5
count is 6
count is 7
count is 8
count is 9
count is 10
...

using this:
Code: [Select]
       
For counter As Integer = 1 To 100
            ed.WriteMessage(String.Format("{0}count is {1}", vbCr, counter))
        Next

you'll get a running percentage, but as mentioned previously you'll need to use 'DoEvents' so the command line can be updated