Question progressbar freezes the main form?

iknowyou

Active member
Joined
Jun 14, 2011
Messages
37
Programming Experience
1-3
I pass progressbar byref to the processing() function on ClassLibrary. My main form freezes when i run this.
How can i prevent my main form by freezing? My Function is always on the class library, so is there any way or any
type of approach to this problem. I need the progressbar to be accurate in showing the process. Please Help.Thanks.


Main Form Code
VB.NET:
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim lib1 As New ClassLibrary1.Class1()
        lib1.processing(ProgressBar1)
    End Sub

Class Library Code
VB.NET:
Public Class Class1
    Public Sub processing(ByRef progBar As Windows.Forms.ProgressBar)
        progBar.Minimum = 0
        progBar.Maximum = 1000000
        For i As Integer = 0 To 1000000
            progBar.Value = i
        Next
    End Sub
End Class
 
Try this:

Public Class Class1
    Public Sub processing(ByRef progBar As Windows.Forms.ProgressBar)
        progBar.Minimum = 0
        progBar.Maximum = 1000000
        For i As Integer = 0 To 1000000
            progBar.Value = i
            Application.DoEvents()
        Next
    End Sub
End Class
 
I pass progressbar byref to the processing() function on ClassLibrary. My main form freezes when i run this.
How can i prevent my main form by freezing? My Function is always on the class library, so is there any way or any
type of approach to this problem. I need the progressbar to be accurate in showing the process. Please Help.Thanks.


Main Form Code
VB.NET:
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim lib1 As New ClassLibrary1.Class1()
        lib1.processing(ProgressBar1)
    End Sub

Class Library Code
VB.NET:
Public Class Class1
    Public Sub processing(ByRef progBar As Windows.Forms.ProgressBar)
        progBar.Minimum = 0
        progBar.Maximum = 1000000
        For i As Integer = 0 To 1000000
            progBar.Value = i
        Next
    End Sub
End Class
I think you have your logic backwards, what you should be doing is using a BackgroundWorker and be reporting the processing from there to update your form, instead of passing a reference of the ProgressBar to the class that does the work.
 
Thanks, although Im not familiar with BackgroundWorker, I try to search and learn that. I think that's the answer Im looking for. Thank you so much.
 
I dont think backgroundworker is capable of doing the process that inside a ClassLibrary or Im not getting something? Please help.
 
I dont think backgroundworker is capable of doing the process that inside a ClassLibrary or Im not getting something? Please help.
Sure you can, whatever calls you make inside the DoWork event will be ran on the thread, the biggest problem you're going to have is that you'll need to pass a reference to the background worker to the class' method so you can call the BW's ReportProgress method.

Also make sure the BW's ReportsProgress property is set to true too.
 
Back
Top