Question BackgroundWorker and progress window

Swoop

Member
Joined
Dec 7, 2011
Messages
14
Programming Experience
Beginner
I've created a dialog which I'd like to popup from any function or sub in any class or form that I have in a project. It would nice to have it work exactly like a messagebox except it's got a progressbar and a cancel button on it. It's job is to display the progress of other functions so it should just stay open until I close it or the user selects cancel.

Now I've found many of the basic examples on the net and have got the dialog working exactly as they have shown it. The trouble is that the examples are using a counter or thread.sleep which simply adds +1 in the DoWork sub. In other words, it validates that a worker thread can update a progressbar but it's a closed loop that only exists in the dialog's class.

The examples don't show how to call an unkown function in another class dynamically, they're simply a hard coded 1+1 in DoWork. To me it's not a real world example because it doesn't show how to plug into the dialog or get a communication stream from other classes and functions that are running and wanting to increment the progressbar.

I suppose that the sequence of events should be as follows...

Create an instance of the progressbar dialog
Set a property (or something) which allows it to call a function which would only be know at runtime ***
Display the dialog
Start the backgroundworker...i.e...async behavior
From the DoWork sub call the function that was set at runtime ***
Update the progresssbar
Complete the function
Close the dialog

What am I missing in the code examples from the net to be able to do this seemingly standard way of using a progress dialog? My goodness...any help would be greatly appreciated!


VB.NET:
'Example from the net...
'This seems to be a closed loop that doesn't do anything except update the progress bar so you can watch it change for the example...
'Useless or is it just me?

Private Sub bgw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
     
        For i As Integer = 1 To 100

            If bgw.CancellationPending Then
                e.Cancel = True
                Exit For
            End If

            System.Threading.Thread.Sleep(250)
            bgw.ReportProgress(CInt((i / m_CountTo) * 100))
            SetLabelText_ThreadSafe(lblStatus, FormatPercent(i / m_CountTo, 2))

        Next

    End Sub
 
Last edited:
Alright, figured this one out. Just had to make the dialog non-modal but act like a modal dialog. Thanks for the help...
 
Setlabeltext_threadsafe is not needed. Use the other overload of btw.reportprogress that takes int,object and pass the new label text as the object. Use it in the progress changed event handler
 
Ps the code sample is not useless. The "work" it is doing is i=i+1

if you were to put other code here instead for example "read,replacetext,write " 500 files then it would be more "useful" in your definition but really there's no difference
 

Latest posts

Back
Top