How to display cancelable progress bar when executing some function from the Ribbon.

504 views
Skip to first unread message

Gabor Siklos

unread,
May 24, 2018, 2:22:40 PM5/24/18
to Excel-DNA
I know this has been asked before and I did search the forums and read the responses but there hasn't been any (semi) implemented solutions offered (at least I couldn't find one).

I'm have a ribbon button that pops up a WPF forms that takes some user input and when the user clicks "Ok" executes a (potentially) long running (.net) function, which upon completion, writes some data on the sheet, using COM. I'd like to be able to display a simple progress bar window with a Cancel button. How exactly do I do this? Right now all of this is sequential, and synchronous. Do I have to wrap the execution of the function in some async wrapper? The more detail, the better and a partial solution would be absolutely most welcome! 

Thanks a lot in advance!

-Gabor

Govert van Drimmelen

unread,
May 24, 2018, 4:04:35 PM5/24/18
to exce...@googlegroups.com
This repo explores ideas area UI in an Excel add-in, including threading options

-Govert
--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To post to this group, send email to exce...@googlegroups.com.
Visit this group at https://groups.google.com/group/exceldna.
For more options, visit https://groups.google.com/d/optout.

Gabor Siklos

unread,
May 24, 2018, 4:19:21 PM5/24/18
to Excel-DNA
Thanks Govert. I've seen that repo when I was looking for a RefEdit emulation but I'm not sure how that would help with the progress bar question. I probably don't know enough about how threading in Excel works or maybe didn't describe the issue appropriately enough. For the progress bar, I don't need Excel to be "responsive" in a sense that I don't need it to allow clicking in other sheets, for example. My whole purpose with the progress bar is to provide the user a sense that some calculation is going on and to give her the chance to cancel. Right now, once the "OK" button is clicked on my form, Excel essentially "freezes" until the computation is finished and the results are written out to the sheet. 
Message has been deleted
Message has been deleted

Vladimir Krilov

unread,
May 26, 2018, 3:51:28 AM5/26/18
to Excel-DNA
Hi Gabor!

Earlier someone posted here a similar solution and I made a small reference solution based on it for myself. Please find it attached (restore packages before building). The only difference from your needs is that it starts the operation right after clicking corresponding ribbon button and you need to show a form before starting the update. Please let me know if you need more help.

Thanks,
Vladimir
TestXLL.zip

Gabor Siklos

unread,
May 31, 2018, 4:47:52 PM5/31/18
to Excel-DNA
Thanks a lot Vladimir for that reference project! That was a great help. I tested it and I'm in the process of incorporating it into my project, however there's one slight problem with it: the progress bar window doesn't close after it's run to completion. I see that a call to "Close" is dispatched but somehow it still doesn't. This is happening both in your reference project as well as in my project. Do you see the same behavior?

On another note, I was wondering if this could be extended or if somebody has any suggestions about how to implement a similar thing but more of a "waiting bar" as opposed to a progress bar. A lot of times, I have a situation where I'd like to execute one long running task and I don't have to ability to intersperse it with update statements for the progress bar. For example, if you want to call a web service. This, I think is usually done with a progress bar and a background worker but I don't know if you can do something like that with Excel's threading model.

Thanks a lot again,

-Gabor

Vladimir Krilov

unread,
Jun 1, 2018, 1:57:26 AM6/1/18
to Excel-DNA
Gabor, thanks for reporting back, something definitely goes wrong.

As a temporary workaround please call Complete twice:

(ExcelDnaUtil.Application as Application).Interactive = true;
ProgressBarManager.Complete();
ProgressBarManager.Complete();
MessageBox.Show(excelWindow, "Finished running action.", "Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);

I'll try to look into this once I have time.

Thanks,
Vladimir

Vladimir Krilov

unread,
Jun 1, 2018, 7:31:27 AM6/1/18
to Excel-DNA
Please see updated solution attached.

Thanks,
Vladimir
TestXLL.zip

Vladimir Krilov

unread,
Jun 1, 2018, 7:45:17 AM6/1/18
to Excel-DNA
... as for "waiting bar" you can show a control with some storyboard animation in the progress window (which repeats forever), then just close the window when you're done.

Vladimir Krilov

unread,
Jun 1, 2018, 2:36:56 PM6/1/18
to Excel-DNA
Also, you can just set "IsIndeterminate" property to True on the Progress Bar control and skip reporting progress altogether.

Gabor Siklos

unread,
Jun 1, 2018, 4:57:29 PM6/1/18
to Excel-DNA

Thank Vladimir again for all the help and the corrected example solution. I actually came up with a very similar fix after debugging it myself as well. Would setting the IsIndeterminate property of the ProgressBar circumvent the user to be able to cancel the task, though?


Vladimir Krilov

unread,
Jun 1, 2018, 11:31:05 PM6/1/18
to Excel-DNA
No. Just try to set the property in xaml without any other changes and see how it works :).

Vladimir Krilov

unread,
Jun 2, 2018, 2:09:05 PM6/2/18
to Excel-DNA
... you can comment out ProgressBarManager.UpdateProgress((double)i / n) calls in this case.

To keep the ability to cancel the task, you still need to pass the cancellation token into your long operation and check cancellation somewhere inside this operation (token.ThrowIfCancellationRequested()). So the ability to cancel the task doesn't directly relate to reporting progress.

Thanks,
Vladimir

Gabor Siklos

unread,
Jun 4, 2018, 9:48:07 AM6/4/18
to Excel-DNA
Thank you Vladimir for the tremendous help!
Reply all
Reply to author
Forward
0 new messages