Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Form Refesh not working under Threading

Reply
 
Old 02-18-2009   #1 (permalink)
John


 
 

Form Refesh not working under Threading

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



My System SpecsSystem Spec
Old 02-19-2009   #2 (permalink)
John


 
 

Re: Form Refesh not working under Threading

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" <FTM@xxxxxx> wrote in message
newsF69663A-3DED-45D2-AE54-B13D110A0339@xxxxxx
Quote:

> "John" <non@xxxxxx> wrote in message
> news:OVpKq0ikJHA.4880@xxxxxx
Quote:

>>
>> 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.
>
>
> --
> Mike
>

My System SpecsSystem Spec
Old 02-19-2009   #3 (permalink)
Cor Ligthert[MVP]


 
 

Re: Form Refesh not working under Threading

John,

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

Cor

"John" <non@xxxxxx> wrote in message
news:OVpKq0ikJHA.4880@xxxxxx
Quote:

> 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
>
My System SpecsSystem Spec
Old 02-19-2009   #4 (permalink)
John


 
 

Re: Form Refesh not working under Threading

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
"Family Tree Mike" <FTM@xxxxxx> wrote in message
newsF69663A-3DED-45D2-AE54-B13D110A0339@xxxxxx
Quote:

> "John" <non@xxxxxx> wrote in message
> news:OVpKq0ikJHA.4880@xxxxxx
Quote:

>>
>> 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.
>
>
> --
> Mike
>

My System SpecsSystem Spec
Old 02-19-2009   #5 (permalink)
John


 
 

Re: Form Refesh not working under Threading

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

"Cor Ligthert[MVP]" <Notmyfirstname@xxxxxx> wrote in message
news:OsRwn$mkJHA.1288@xxxxxx
Quote:

> John,
>
> You cannot reach any UI from a worker thread, you need to make some
> communication to your main thread.
>
> Cor
>
> "John" <non@xxxxxx> wrote in message
> news:OVpKq0ikJHA.4880@xxxxxx
Quote:

>> 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
>>
>

My System SpecsSystem Spec
Old 02-19-2009   #6 (permalink)
John


 
 

Re: Form Refesh not working under Threading

Worked great thanks for your help
John
"Family Tree Mike" <FTM@xxxxxx> wrote in message
news:6A2708C0-16E9-42B6-85F1-EE8A6B3F5CA7@xxxxxx
Quote:

> "John" <non@xxxxxx> wrote in message
> news:u9$2gzmkJHA.4912@xxxxxx
Quote:

>> 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" <FTM@xxxxxx> wrote in message
>> newsF69663A-3DED-45D2-AE54-B13D110A0339@xxxxxx
Quote:

>>> "John" <non@xxxxxx> wrote in message
>>> news:OVpKq0ikJHA.4880@xxxxxx
>>>>
>>>> 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.
>>>
>>>
>>> --
>>> Mike
>>>
>>
>>
>
>
> 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.
>
>
> --
> Mike
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Threading problem .NET General
Threading Disappeared... Live Mail
Threading problem .NET General
E-mail: threading Live Mail
Strict Threading Vista mail


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46