Addin Progress Bar not updating

Addin Progress Bar not updating

SteveLainson6531
Contributor Contributor
1,087 Views
2 Replies
Message 1 of 3

Addin Progress Bar not updating

SteveLainson6531
Contributor
Contributor

Hello All,

 

I'm still on the long journey trying to convert a simple Inventor macro to an Inventor Addin. Its been a painful learning experience and my code is now much, much longer than was in the original ivb. I'm beginning to wish I'd never started but I'm determined to get it working properly.

 

My latest problem is that my forms progress bar ("MyProgBar" does not update until all the work is completed. In my macro I used a shaded label, just changing the width to represent the progress, however I've been tempted to use the vb.net progress bar for my Addin but it does work as expected as it doesnt update until the Addin has finished running.

 

It's not stopping my Addin from working, but just a bit annoying...

 

So, here is my code:

       For Each oOcc In oLeafOccs
            Dim oCompDef As ComponentDefinition
            Dim oDocument As Document

            oCompDef = oOcc.Definition
            oDocument = oCompDef.Document

            ' check the type of the document
            If oDocument.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
                oPartDoc = oDocument 'set the part
                oSheet = oBook.Worksheets.Add()   'add a new sheet for this part
                oSheet.Name = oPartDoc.DisplayName  'rename the sheet to the Inventor part name

                If o3DSP = True Then Export3DSketchPoints(oPartDoc) 'extract the 3DSketch Points and write to the SHEET
                If o3DWP = True Then Export3DWorkPoints(oPartDoc) 'extract the 3DSketch Points and write to the SHEET
            End If
            MyProgBar.Value = MyProgBar.Value + 1
            MyProgBar.Refresh()
            System.Windows.Forms.Application.DoEvents()
        Next oOcc

When the Addin runs the progress bar does not update until AFTER everything has completed. DoEvents() would be my usual .net rescue in such situations, but here it seems to have no effect. Neither does .Refresh().

 

Any suggestions?

 

TIA

 

 

 

 

 

 

0 Likes
1,088 Views
2 Replies
Replies (2)
Message 2 of 3

Owner2229
Advisor
Advisor

Hi, maybe this will do it:

 

 

'I'm ussing this one to update the window-type progressbar
MyProgBar.Message = ("Done: " & Round(Amount * j) & "/" & "100%")
MyProgBar.UpdateProgress
'But this should do it
MyProgBar.Value = MyProgBar.Value + 1
MyProgBar.UpdateProgress

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 3 of 3

rjay75
Collaborator
Collaborator

I've had similar problems getting progress bars to update when using Windows Forms with Inventor and standalone applications. I actually learned a solution for updating it while working on Android Applications. In Android apps the recommend way is doing long running tasks in a separate task or thread from the UI. Then updating the UI using a function which sends and update 'message' back to the main UI thread. 

 

The issue is Windows Forms won't update until the current method completes. If there's a long running process it must complete before the controls will repaint themselves. So though the progress bar is updating it's not visually apparent.

 

Here's a an MSDN article showing one way it can be done. The downside is it can seem like a lot of work to update a progress bar. The upside is you have nice responsive UI that updates smoothly.

 

Another way is could be to use an asynchronous method to do the work. 

 

In all of this you have to be careful of how you do any threading with Inventor addins as sometimes you can end up with wierd threading errors when trying to envoke some Inventor methods from other threads.

 

The simplest thing will be to use Inventor's ProgressBar in the status bar. The API help has an example in it of using the progressbar.

 

Here's a sample ilogic rule using the API example.

 

SyntaxEditor Code Snippet

Imports System.Threading

Public Sub Main()
    Dim iStepCount As Long
    iStepCount = 50

    ' Create a new ProgressBar object.
    Dim oProgressBar As ProgressBar
    oProgressBar = ThisApplication.CreateProgressBar(True, iStepCount, "Test Progress")

    ' Set the message for the progress bar
    oProgressBar.Message = "Executing some process"

    Dim i As Long
    For i = 1 To iStepCount
        ' Sleep 0.2 sec to simulate some process
        Thread.Sleep(200)
        oProgressBar.Message = "Executing some process - " & i
        oProgressBar.UpdateProgress
    Next

    ' Terminate the progress bar.
    oProgressBar.Close

End Sub

 

0 Likes