For a Visio VSTO add-in, what is a good progress bar option

Started by Visisthebest, August 15, 2023, 10:59:09 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Visisthebest

I am adding some user options to a Visio VSTO add-in that may take seconds to about a minute to execute on older laptops.

I would like to add a simple progress bar. Let's say the user is calculating through about 20 workflows in the user action, I want to update the percentage complete and counter of the amount of workflows completed every time one workflow calculation is completed.

(so it doesn't have to be accurate, as some workflows may be much bigger than other, but the user now knows Visio is doing calculations and not hanging)

What is the simplest option for a progress bar for a Visio add-in that gives the least problems, something simple that just works and is predictable is preferred to a fancy progress bar.

Thank you for sharing your ideas!
Visio 2021 Professional

Nikolay

The problem here is not the progress bar (its' choice is pretty much defined by the UI library you are using, for the "default" winforms there is only one progress bar control).
The actual issue here is showing a form in the foreground that can react to user clicks, like "cancel" button (and show/animate that progress bar), while running job in the backgound

I have used several approaches for this in the past.

First: use a background worker thread (in .NET it can be BackgroundWorker). Problem: inefficient (will make things even slower).
Because Visio calls will be executed from a secondary thread, then posted to the main thread, then results back. For you, it will be transparent, though.

Second: launch a second invisible Visio instance in the background, and do all the job there.
From the second instance, notify to the first one about the progress (the second instance will also load your add-in, so they can communicate, i.e. you can send "jobs" to your add-in in the second instance, and that "agent" can send back progress)
Problem: hard to implement

Third: use DoEvents
Like, create a non-modal progress form, update it periodically, and call DoEvetns in your code so that it can redraw itself. Pretty similar to standard VBA approach.
Problem: inefficient.

Fourth: use forced repaint (RedrawWindow). The form will be not responsive, but it will show progress bar and this is the fastest method.
Problem: window will be semi-frozen during the operation.

Visisthebest

Hi Nikolay, thank you for the overview good to know there is no straightforward solution.

This one sounds like the one I'd go for:

Third: use DoEvents
Like, create a non-modal progress form, update it periodically, and call DoEvetns in your code so that it can redraw itself. Pretty similar to standard VBA approach.
Problem: inefficient.

I wonder what kind of inefficiency it creates, the last option that semi-freezes Visio doesn't sound like the behavior I'd want. (I want to avoid the user thinking Visio is freezing).

I understand that by calling DoEvents and letting the form redraw itself (frequently) I avoid the user clicking on Visio and pushing it in to the background? Do I understand it correctly?

Visio 2021 Professional

Nikolay

Yes, correct DoEvetns processes all events and redraws, thus wasting precious time :)
But other than that it should be harmless.

So assuming you are using winforms, added a Form "Form1" and dropped a progress bar control "ProgressBar1" to it:

        Dim steps = 10 ' number of steps

        Dim form = New Form1()
        form.ProgressBar1.Minimum = 0
        form.ProgressBar1.Maximum = steps

        form.Show()
        For i = 0 To steps
            Threading.Thread.Sleep(1000) ' do something here

            form.ProgressBar1.Value = i
            System.Windows.Forms.Application.DoEvents()
        Next
        form.Hide()


There may be some other issues to take care of, like disable closing the form, or brining it to the foreground.
But those are different.

Visisthebest

Thank you Nikolay for the sample code, I have done very little with WinForm so far will let you know if I get it to work properly! (I will though)

"disable closing the form, or bringing it to the foreground", and things like scaling (a hassle with WinForms I understand) I'll be able to deal with.
Visio 2021 Professional

Visisthebest

Visio 2021 Professional