Run ProgressBar through Thread Problem

osamaakhattab

New member
Joined
Feb 8, 2006
Messages
4
Programming Experience
1-3
Dear All:
I have major problem which could be summarised as follow:
I have main form which has Progressbar and i attached Contextmenue Strip to that form which Start and Stop the Progressbar . when i click start it must start increment the progress bar and when i click stop it will stop it. when i click start i cant press righ click mouse the form will freez i tried to do thread but even though it dosnt work :
This is the code and i hope any one can help

VB.NET:
Private Sub StartDepreciation_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles StartDepreciation.Click
Try
 
Dim TestMe As New DepreciateNow(Me)
Dim s As New Thread(AddressOf TestMe.StartStockDepreciation)
s.SetApartmentState(ApartmentState.STA)
s.IsBackground = True
 
's.IsBackground = True
'Thread.VolatileRead(Me)
s.Start()
' SetProgressBar()
' StartStockDepreciation()
Catch ex As Exception
End Try
End Sub
 
 
 
Public Class DepreciateNow
 
Public Sub New(ByVal f As Form)
formMine = CType(f, frmStockDepreciationConfig)
End Sub
Public Sub New()
 
End Sub
Public Delegate Sub TestDel()
 
Public Sub StartStockDepreciation()
Dim Delegated As New TestDel(AddressOf StartStockDepreciation)
'if in
If CType(formMine, frmStockDepreciationConfig).InvokeRequired Then
CType(formMine, frmStockDepreciationConfig).Invoke(Delegated)
Else
 
 
Dim blDepreciating As Boolean = False
Dim s As New frmStockDepreciationConfig
Dim dsStock As New DataSet
Dim intStockID, intNumRows, intRow As Int32
Try
'Check there are stock items in the stock table
aNames.Clear()
aValues.Clear()
dsStock = clsHolisTech.Execute("usp_STC_List_Stock_Global", aNames, aValues)
If dsStock.Tables(0).Rows.Count < 1 Then
MessageBox.Show("There are no Stock Items to Depreciate.", _
"Aborting Depreciation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
dsStock.Dispose()
Exit Sub
End If
intNumRows = dsStock.Tables(0).Rows.Count
intRow = 0
blDepreciating = True
 
CType(formMine, frmStockDepreciationConfig).cmsMain.Items("StartDepreciation").Enabled = False
'frmStockDepreciationConfig.cmsMain.Items("StartDepreciation").Enabled = False
CType(formMine, frmStockDepreciationConfig).cmsMain.Items("StopDepreciation").Enabled = True
While intRow < intNumRows And blDepreciating = True
intStockID = CType(dsStock.Tables(0).Rows(intRow).Item("StockID"), Int32)
Try
'Call Depreciate function from StockSharedFunctions class to depreciate a single stock item
StockSharedFunctions.DepreciateStock(intStockID)
Catch
MessageBox.Show("There was an unexpected problem Depreciating Stock ID " _
& intStockID.ToString & ". Please record this number and contact your System Adminstrator for assistance.", _
"Depreciation Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
 
'
''Start Thread Which will initialise the Progress Bar 
'
 
'Dim ConnectionThread As New Thread(AddressOf test)
'ConnectionThread.SetApartmentState(ApartmentState.STA)
'ConnectionThread.Start()
'ConnectionThread.IsBackground = True
CType(formMine, frmStockDepreciationConfig).UserControl11.ProgressBar1.Increment(1)
intRow = intRow + 1
' ConnectionThread.Suspend()
'pbarProgress.Value = intRow
'pbarProgress.Refresh()
End While
 
Catch ex As Exception
End Try
 
End If
End Sub
end class
 
Last edited by a moderator:
The first thing you do in that threaded class method is to check InvokeRequired on the main thread, which of course is True, then you invoke the method as a delegate instance on main thread - you take yourself back to square one right away, you never do that method in a separate thread in reality. Which explains that main thread in inresponsive while the method runs. The only things you need to invoke on is objects that belongs to (created in) main thread. In your case you could make a method in DepreciateNow class that updates progressbar and only invoke this method back to main thread.

It looks like the cmsMain.Items of main form should be set up outside the class before you start it asynchronously.

I didn't see 'formMine' declared in that class, but gather this was just a copy&paste operation for you.

Please use the code boxes for code blocks next time, makes posts much more readable.
 
Back
Top