Macros too slow !

  • Thread starter Thread starter Cerebrus
  • Start date Start date
C

Cerebrus

Hi all,

I posted this topic in microsoft.public.dotnet.general but received no
reply. So hoping for a reply here...

I'm trying to use a macro in VS 2003 to insert a code snippet in
various sections of my code. This code will help me to test the time
taken for any particular action to be performed.

The problem is that it takes nearly 30 seconds for it to be executed,
after I press my Keyboard shortcut, and to see the results in my code
window. I'm new to using macros in the IDE, so I have no idea if the
macro is actually being compiled and then executed or what !

The macro code is as follows :
=========================
Sub TimeSpanCheck()
Dim objDoc As Document = DTE.ActiveDocument
Dim mySel As TextSelection = objDoc.Selection
mySel.Text = "Console.Write(""Time before the action :"")"
mySel.NewLine()
mySel.Text = "Dim oldTime As DateTime = DateTime.Now"
mySel.NewLine()
mySel.Text = "Console.WriteLine(""{0:HH:mm:ss.fff}"", oldTime)"

mySel.NewLine()
mySel.Text = " ' "
mySel.NewLine()
mySel.Text = " ' My actions here"
mySel.NewLine()
mySel.Text = " ' "
mySel.NewLine()
mySel.Text = "Console.Write(""Time after the action :"")"
mySel.NewLine()
mySel.Text = "Dim newTime As DateTime = DateTime.Now"
mySel.NewLine()
mySel.Text = "Console.WriteLine(""{0:HH:mm:ss.fff}"", newTime)"

mySel.NewLine()
mySel.Text = "Dim Diff As TimeSpan = newTime.Subtract(oldTime)"

mySel.NewLine()
mySel.Text = "Console.WriteLine(""Time taken for process :
{0}min:{1}secs:{2}millisecs"", Diff.Minutes, Diff.Seconds,
Diff.Milliseconds)"
mySel.NewLine()
objDoc.Save()
End Sub
=========================


Am I doing something wrong here, or is this time taken normal ? Also,
is there a way to speed it up (something like precompiling the code.)


Any comments are appreciated,


Regards,


Cerebrus.
 
Hi,

You are using TextSelection.Text property to insert the text. This may
be the reason of slow execution. The better way is to use
TextSelection.Insert method. See my blog on VS macros at
http://www.vbdocman.com/blog/archives/14. This article contains very
simple macro which inserts some code at the current cursor position. In
addition it correctly handles Undo operation. It executes immediately.

FYI, you can find more responses about for VS add-ins and macros in
microsoft.public.vb.addins.
 
Hi Peter,

AFAIK, microsoft.public.vb.addins is for VB6 add-ins, not for VS.NET
add-ins. I used to answer questions in that newsgroup long time ago...

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
 
Wow ! Thanks a lot, Peter.

After checking your sample, I realized that I could use all the
Framework classes here. So I used a StringBuilder to build up the Code
fragment and then used ONE insert statement only. I also learnt how to
use Undo.

Now it works, and it is instantaneous ! ;-)

Thanks again,

Regards,

Cerebrus.
 
Back
Top