0
votes

I was wondering if anyone has any tips on how to speed up automation in a Visio project in C#. The automation I have is building toilet stalls. I end up choosing to make 10 stalls, VBA takes about 3 seconds to make all the shapes.

I just finish coverting the VBA into a c# Form Application using the Microsoft.Office.Interop.Visio library. To do the same test, it takes 14 seconds.

I've been able to take off 3 seconds by making the visio application invisible and then making it visible after its done, but that is still not even close to what VBA does.

I've noticed with C# I can see the visio drawing building itself with each shape while in VBA it does not. So I also tried using: "Application.ScreenUpdating". In c# it takes a short data type so to make it false I tried making it 0, and it did nothing. Also I tried turning it on in VBA using "True" but that didn't turn it on.

So anymore more tips on making it faster? I'm surprised that VBA is faster than C#(maybe because its built into the Office Suite).

1
You can never beat code that runs inside the Visio process. A Winforms project will always get hammered by the fundamental cost of having to cross the process boundary and requiring two thread-context switches. A very simple property getter runs roughly 10,000x slower due to this overhead. It hurts a lot less when the interop member is beefy. You'll have to create an add-in to create comparable code. - Hans Passant
this is not a VBA question then. please delete the VBA tag. - jsotola
If you drop masters programmatically you can use the dropmany command and that can save a lot of time. You may have to get creative to use a master if you’re currently just drawing geometry though. - Jon Fournier

1 Answers

1
votes

First, you cannot beat VBA at what it's the best... Like Hans Passant wrote, you loose a lot on interop. Especially if it is out-of-process call, this will be many times slower than in-process VBA call. Addin will help (the code will run in-process), but VBA will still be faster, as still you have interop (assuming a dotnet add-in - Visio is a native app, so you will have COM interop under the hood anyways)

Option one is to do it in vba. Most people choose it. Means you forget about c# and your windows forms application.

Option two is to minimize interop (minimize number of Visio calls). Also, Here are some hints https://blogs.msdn.microsoft.com/mailant/2004/09/22/dev-luv-visio-development-top-five-performance-tips/

Most of them apply to both vba and c# though. And you will not beat vba anyways, even if you follow all of them. Note that you can make VBA code much faster, if you follow them.

One more option is to generate Visio file without Visio (as open xml). With that, you can beat VBA performance. But the amount of code you will need to write will be probably 20 times more than that of VBA, and you nèed to know some visio intervals.