Form Refesh not working under Threading

J

John

If I call the "SyncTables" Sub without a Thread all the code works fine and
with the Thread I'm not getting any errors but the ProgressBar & Label
referenced
in the last "Class SyncChartofAcc" no longer updates on the Refesh. I
removed all the Progress loops to simplify the code.
Thanks in advance
John

Public Class WebTimer

Private WithEvents myBackgroundWorker As New
System.ComponentModel.BackgroundWorker
Private Delegate Sub ProgressHandler()
Dim updateHandler As ProgressHandler = Nothing

Private Sub btnSync_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSync.Click

Dim t As Thread
t = New Thread(AddressOf SyncTables)
t.Start()

End Sub



Private Sub DoWork()
pgbSync.Step = 8
pgbSync.PerformStep()

End Sub



Private Sub SyncTables()

pgbSync.Maximum = 100
updateHandler = New ProgressHandler(AddressOf DoWork)

'Call Class
Dim SyncChart As New SyncChartOfAcc
SyncChart.DoAccountQueryRq("US", 5, 0, O, qbFileLoc, CompID)

'This Progress update is working under Thread
If pgbSync.InvokeRequired Then
Invoke(updateHandler)
Else
pgbSync.PerformStep()
End If

End Sub

End Class



Public Class SyncChartOfAcc
Public Sub DoAccountQueryRq(ByVal country As String, ByVal majorVersion As
Short, ByVal minorVersion As Short _
, ByVal O As Integer, ByVal qbFileLoc As
String, ByVal CompID As String)

'This Progress update is not working under Thread
Dim prg As ProgressBar = WebTimer.pgbSegmentSync
Dim lblProg As Label = WebTimer.lblSyncSegPrg
prg.Maximum = 100
prg.Step = 1
lblProg.Text = "Getting Chart of Accounts"
WebTimer.Refresh()


'loops removed to simplify
prg.PerformStep()
lblProg.Text = "Processing Account Number " & (QBList.Count) - c
WebTimer.Refresh()


End Sub

End Class
 
F

Family Tree Mike

John said:
Public Class SyncChartOfAcc
Public Sub DoAccountQueryRq(ByVal country As String, ByVal majorVersion As
Short, ByVal minorVersion As Short _
, ByVal O As Integer, ByVal qbFileLoc As
String, ByVal CompID As String)

'This Progress update is not working under Thread
Dim prg As ProgressBar = WebTimer.pgbSegmentSync
Dim lblProg As Label = WebTimer.lblSyncSegPrg

WebTimer is a class, not an object. Does this really work?

When I changed the code to what I think this should be, adding an instance
of the WebTimer class in the class SyncChartOfAcc, I get a cross thread
error, which I would expect.
 
J

John

Yes I stepped thru the code and tried adding a new Instance also and still
no error, but no Refresh on the WebTimer form either.

I tested for InvokeRequired and it returns false.
These are my Imports
Imports System.Threading
Imports System.Configuration

Thanks
John
 
C

Cor Ligthert[MVP]

John,

You cannot reach any UI from a worker thread, you need to make some
communication to your main thread.

Cor
 
J

John

I added a WebTmer.Show to see what happens, it opens a 2nd instance of the
Form, which does show an active progressbar, it then closes when the code is
done leaving the original WebTimer Form.

just relating what I find
John
 
J

John

Thanks I just started to replicate what is working on the first Progress
with the New Thread doing just what you are saying, this is what I got so
far but getting a:
Invoke cannot be called on a control until a Window Handle has been created

'Call back to main thread
Dim webT As New WebTimer
webT.ProgressSync()
'Main thread
Public Sub ProgressSync()
updateHandler = New ProgressHandler(AddressOf DoWorkSync1)
'getting windows Handle error next line
Invoke(updateHandler)
End Sub

Private Sub DoWorkSync1()
pgbSegmentSync.Maximum = 100
pgbSegmentSync.Step = 1
pgbSegmentSync.PerformStep()
lblSyncSegPrg.Text = "Getting Chart of Accounts"
End Sub
 
F

Family Tree Mike

John said:
Yes I stepped thru the code and tried adding a new Instance also and still
no error, but no Refresh on the WebTimer form either.

I tested for InvokeRequired and it returns false.
These are my Imports
Imports System.Threading
Imports System.Configuration

Thanks
John

Family Tree Mike said:
WebTimer is a class, not an object. Does this really work?

When I changed the code to what I think this should be, adding an
instance of the WebTimer class in the class SyncChartOfAcc, I get a cross
thread error, which I would expect.


Don't add a new instance, but instead pass the current instance of the
WebTimer to your SyncChartOfAcc instance in sub SyncTables.

Something like:

dim SyncChart as new SyncChartOfAcc
SyncChart.WT = Me

then in SyncChartOfAcc:

public WT as WebTimer ' better to use a property than a field.

and then in DoAccountQueryRq use WT rather than WebTimer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top