473,322 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Thread, UI update is this ok?

Hi group,

I have been doing some reading on threading and updating the ui from a
worker thread and I made this sample which works but I was wondering if it's
the ok way to do it? Can I improve something? What's the best practise for
updating multiple controls on my form for example 5 labels or so, just keep
passing them to the constructor?

Greetz, Peter

On my Form:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim oMyUpd As New MyUpdater(ProgressBar1, lblPct)
oMyUpd.Start()

End Sub

'The worker thread

Imports System.Threading

'================================================= =====================
Public Class MyUpdater
Delegate Sub UpdateStarter()
Delegate Sub FeedBack(ByVal myValue As Integer)

Private oThread As Thread
Private oMyUpdate As New UpdateStarter(AddressOf StartUpdating)

Private progy As ProgressBar
Private infoLbl As Label

Public Sub New(ByVal myProg As ProgressBar, ByVal myLabel As Label)
progy = myProg
infoLbl = myLabel
End Sub

Public Sub [Start]()
oMyUpdate.BeginInvoke(Nothing, Nothing)
End Sub

Public Sub StartUpdating()
Dim giveFeedBack As New FeedBack(AddressOf IncreaseValue)
oThread = Thread.CurrentThread
For i As Integer = 0 To 15
progy.BeginInvoke(giveFeedBack, New Object() {i})
Thread.Sleep(200)
Next

End Sub

Public Sub IncreaseValue(ByVal myVal As Integer)
progy.Value = myVal
infoLbl.Text = CStr(Math.Round(myVal / progy.Maximum * 100, 2))
End Sub
End Class

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)
Oct 5 '06 #1
12 1223
In article <uJ**************@TK2MSFTNGP02.phx.gbl>,
pp*****@nospam.hotmail.com says...
Hi group,

I have been doing some reading on threading and updating the ui from a
worker thread and I made this sample which works but I was wondering if it's
the ok way to do it? Can I improve something? What's the best practise for
updating multiple controls on my form for example 5 labels or so, just keep
passing them to the constructor?

Greetz, Peter

On my Form:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim oMyUpd As New MyUpdater(ProgressBar1, lblPct)
oMyUpd.Start()

End Sub

'The worker thread

Imports System.Threading

'================================================= =====================
Public Class MyUpdater
Delegate Sub UpdateStarter()
Delegate Sub FeedBack(ByVal myValue As Integer)

Private oThread As Thread
Private oMyUpdate As New UpdateStarter(AddressOf StartUpdating)

Private progy As ProgressBar
Private infoLbl As Label

Public Sub New(ByVal myProg As ProgressBar, ByVal myLabel As Label)
progy = myProg
infoLbl = myLabel
End Sub

Public Sub [Start]()
oMyUpdate.BeginInvoke(Nothing, Nothing)
End Sub

Public Sub StartUpdating()
Dim giveFeedBack As New FeedBack(AddressOf IncreaseValue)
oThread = Thread.CurrentThread
For i As Integer = 0 To 15
progy.BeginInvoke(giveFeedBack, New Object() {i})
Thread.Sleep(200)
Next
I like to keep the logic of marshalling between threads local to the
method that is accessing the UI. Like this (haven't coded in VB.NET in
a while so I might be little rusty on the syntax):

Public Sub IncreaseValue(ByVal myVal As Integer)
If progy.InvokeRequired Then
progy.Invoke(New Feedback(AddressOf IncreaseValue), _
new Object() {i})
return
end if

progy.Value = myVal
infoLbl.Text = CStr(Math.Round(myVal / progy.Maximum * 100, 2))
end sub

Now you just need to call IncreaseValue directly in your
"StartUpdating" method -- you don't need the multithreading stuff in
your loop code.

This also has the benefit that if you ever move this method into the UI
thread, the "progy.InvokeRequired" will return false and you'll update
the control right away -- no need to use Invoke. While it's not a huge
expense to call Invoke, why call it when you don't have to? :)

--
Patrick Steele
http://weblogs.asp.net/psteele
Oct 5 '06 #2
Hi,

Thanks for the explanation

Greetz Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"Patrick Steele" <pa*****@mvps.orgschreef in bericht
news:MP************************@msnews.microsoft.c om...
In article <uJ**************@TK2MSFTNGP02.phx.gbl>,
pp*****@nospam.hotmail.com says...
Hi group,

I have been doing some reading on threading and updating the ui from a
worker thread and I made this sample which works but I was wondering if
it's
the ok way to do it? Can I improve something? What's the best practise
for
updating multiple controls on my form for example 5 labels or so, just
keep
passing them to the constructor?

Greetz, Peter

On my Form:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim oMyUpd As New MyUpdater(ProgressBar1, lblPct)
oMyUpd.Start()

End Sub

'The worker thread

Imports System.Threading

'================================================= =====================
Public Class MyUpdater
Delegate Sub UpdateStarter()
Delegate Sub FeedBack(ByVal myValue As Integer)

Private oThread As Thread
Private oMyUpdate As New UpdateStarter(AddressOf StartUpdating)

Private progy As ProgressBar
Private infoLbl As Label

Public Sub New(ByVal myProg As ProgressBar, ByVal myLabel As Label)
progy = myProg
infoLbl = myLabel
End Sub

Public Sub [Start]()
oMyUpdate.BeginInvoke(Nothing, Nothing)
End Sub

Public Sub StartUpdating()
Dim giveFeedBack As New FeedBack(AddressOf IncreaseValue)
oThread = Thread.CurrentThread
For i As Integer = 0 To 15
progy.BeginInvoke(giveFeedBack, New Object() {i})
Thread.Sleep(200)
Next

I like to keep the logic of marshalling between threads local to the
method that is accessing the UI. Like this (haven't coded in VB.NET in
a while so I might be little rusty on the syntax):

Public Sub IncreaseValue(ByVal myVal As Integer)
If progy.InvokeRequired Then
progy.Invoke(New Feedback(AddressOf IncreaseValue), _
new Object() {i})
return
end if

progy.Value = myVal
infoLbl.Text = CStr(Math.Round(myVal / progy.Maximum * 100, 2))
end sub

Now you just need to call IncreaseValue directly in your
"StartUpdating" method -- you don't need the multithreading stuff in
your loop code.

This also has the benefit that if you ever move this method into the UI
thread, the "progy.InvokeRequired" will return false and you'll update
the control right away -- no need to use Invoke. While it's not a huge
expense to call Invoke, why call it when you don't have to? :)

--
Patrick Steele
http://weblogs.asp.net/psteele

Oct 5 '06 #3
Just to make sure, is this what you mean?

Public Sub StartUpdating()
Dim giveFeedBack As New FeedBack(AddressOf IncreaseValue)
oThread = Thread.CurrentThread
For i As Integer = 0 To 15
IncreaseValue(i)
Thread.Sleep(200)
Next

End Sub

Public Sub IncreaseValue(ByVal myVal As Integer)
If progy.InvokeRequired Then
progy.Invoke(New FeedBack(AddressOf IncreaseValue), New Object()
{myVal})
Return
End If

progy.Value = myVal
infoLbl.Text = CStr(Math.Round(myVal / progy.Maximum * 100, 2))
End Sub

Greetz Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"Peter Proost" <pp*****@nospam.hotmail.comschreef in bericht
news:eE**************@TK2MSFTNGP06.phx.gbl...
Hi,

Thanks for the explanation

Greetz Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"Patrick Steele" <pa*****@mvps.orgschreef in bericht
news:MP************************@msnews.microsoft.c om...
In article <uJ**************@TK2MSFTNGP02.phx.gbl>,
pp*****@nospam.hotmail.com says...
Hi group,
>
I have been doing some reading on threading and updating the ui from a
worker thread and I made this sample which works but I was wondering
if
it's
the ok way to do it? Can I improve something? What's the best practise
for
updating multiple controls on my form for example 5 labels or so, just
keep
passing them to the constructor?
>
Greetz, Peter
>
On my Form:
>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim oMyUpd As New MyUpdater(ProgressBar1, lblPct)
oMyUpd.Start()
>
End Sub
>
'The worker thread
>
Imports System.Threading
>
>
'================================================= =====================
Public Class MyUpdater
Delegate Sub UpdateStarter()
Delegate Sub FeedBack(ByVal myValue As Integer)
>
Private oThread As Thread
Private oMyUpdate As New UpdateStarter(AddressOf StartUpdating)
>
Private progy As ProgressBar
Private infoLbl As Label
>
Public Sub New(ByVal myProg As ProgressBar, ByVal myLabel As
Label)
progy = myProg
infoLbl = myLabel
End Sub
>
Public Sub [Start]()
oMyUpdate.BeginInvoke(Nothing, Nothing)
End Sub
>
Public Sub StartUpdating()
Dim giveFeedBack As New FeedBack(AddressOf IncreaseValue)
oThread = Thread.CurrentThread
For i As Integer = 0 To 15
progy.BeginInvoke(giveFeedBack, New Object() {i})
Thread.Sleep(200)
Next
I like to keep the logic of marshalling between threads local to the
method that is accessing the UI. Like this (haven't coded in VB.NET in
a while so I might be little rusty on the syntax):

Public Sub IncreaseValue(ByVal myVal As Integer)
If progy.InvokeRequired Then
progy.Invoke(New Feedback(AddressOf IncreaseValue), _
new Object() {i})
return
end if

progy.Value = myVal
infoLbl.Text = CStr(Math.Round(myVal / progy.Maximum * 100, 2))
end sub

Now you just need to call IncreaseValue directly in your
"StartUpdating" method -- you don't need the multithreading stuff in
your loop code.

This also has the benefit that if you ever move this method into the UI
thread, the "progy.InvokeRequired" will return false and you'll update
the control right away -- no need to use Invoke. While it's not a huge
expense to call Invoke, why call it when you don't have to? :)

--
Patrick Steele
http://weblogs.asp.net/psteele


Oct 5 '06 #4

Yup. And you can get rid of the giveFeedback variable in StartUpdating
since you're no longer using it.

In article <OC**************@TK2MSFTNGP04.phx.gbl>,
pp*****@nospam.hotmail.com says...
Just to make sure, is this what you mean?

Public Sub StartUpdating()
Dim giveFeedBack As New FeedBack(AddressOf IncreaseValue)
oThread = Thread.CurrentThread
For i As Integer = 0 To 15
IncreaseValue(i)
Thread.Sleep(200)
Next

End Sub

Public Sub IncreaseValue(ByVal myVal As Integer)
If progy.InvokeRequired Then
progy.Invoke(New FeedBack(AddressOf IncreaseValue), New Object()
{myVal})
Return
End If

progy.Value = myVal
infoLbl.Text = CStr(Math.Round(myVal / progy.Maximum * 100, 2))
End Sub

Greetz Peter
--
Patrick Steele
http://weblogs.asp.net/psteele
Oct 5 '06 #5
Thanks again, and one more question do you maybe have some good links for
reading up some more on multi threading and ui or just multi threading.

Greetz, Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"Patrick Steele" <pa*****@mvps.orgschreef in bericht
news:MP************************@msnews.microsoft.c om...
>
Yup. And you can get rid of the giveFeedback variable in StartUpdating
since you're no longer using it.

In article <OC**************@TK2MSFTNGP04.phx.gbl>,
pp*****@nospam.hotmail.com says...
Just to make sure, is this what you mean?

Public Sub StartUpdating()
Dim giveFeedBack As New FeedBack(AddressOf IncreaseValue)
oThread = Thread.CurrentThread
For i As Integer = 0 To 15
IncreaseValue(i)
Thread.Sleep(200)
Next

End Sub

Public Sub IncreaseValue(ByVal myVal As Integer)
If progy.InvokeRequired Then
progy.Invoke(New FeedBack(AddressOf IncreaseValue), New
Object()
{myVal})
Return
End If

progy.Value = myVal
infoLbl.Text = CStr(Math.Round(myVal / progy.Maximum * 100, 2))
End Sub

Greetz Peter

--
Patrick Steele
http://weblogs.asp.net/psteele

Oct 5 '06 #6
In article <#9**************@TK2MSFTNGP03.phx.gbl>,
pp*****@nospam.hotmail.com says...
Thanks again, and one more question do you maybe have some good links for
reading up some more on multi threading and ui or just multi threading.
No, sorry. Nothing bookmarked.

--
Patrick Steele
http://weblogs.asp.net/psteele
Oct 5 '06 #7
Peter,

I have no time to test your code, however the Queue class is in my idea
greath to use for multithreading.

You can use an thrown back event or just a timer in your mainthread to see
if that is filled and empty it while using the synclock. Mostly you see a
lot of in my eyes more dificult solutions.

http://msdn2.microsoft.com/en-us/lib...ons.queue.aspx

http://msdn2.microsoft.com/en-us/library/3a86s51t.aspx

Jon Skeet has written a complete page about Multithreading, however in my
eyes he wants to use multithreading to often, there are very few situations
where there is no dependency or another reason that using multithreading has
sense (The only reason I see here often stated is to keep the X (end) button
alive. In my idea is that the horse behind the cart. You should make your
application faster).

(It has only sense as an offline supplier needs more time and you can spread
the proces. By instance if you are downloading and you have a speed of 64Kb
and your supplier 20Gb, than multithreading is whithouth sense. In the
oposite way it can make sense if you are downloading from suppliers which
have all 64Kb lines and you have 20Gb. However in Belgium is the avarage
speed of Internet lines so high that this makes probably as well no sense).

http://www.yoda.arachsys.com/csharp/

I hope this gives some ideas,

Cor


"Peter Proost" <pp*****@nospam.hotmail.comschreef in bericht
news:uJ**************@TK2MSFTNGP02.phx.gbl...
Hi group,

I have been doing some reading on threading and updating the ui from a
worker thread and I made this sample which works but I was wondering if
it's
the ok way to do it? Can I improve something? What's the best practise for
updating multiple controls on my form for example 5 labels or so, just
keep
passing them to the constructor?

Greetz, Peter

On my Form:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim oMyUpd As New MyUpdater(ProgressBar1, lblPct)
oMyUpd.Start()

End Sub

'The worker thread

Imports System.Threading

'================================================= =====================
Public Class MyUpdater
Delegate Sub UpdateStarter()
Delegate Sub FeedBack(ByVal myValue As Integer)

Private oThread As Thread
Private oMyUpdate As New UpdateStarter(AddressOf StartUpdating)

Private progy As ProgressBar
Private infoLbl As Label

Public Sub New(ByVal myProg As ProgressBar, ByVal myLabel As Label)
progy = myProg
infoLbl = myLabel
End Sub

Public Sub [Start]()
oMyUpdate.BeginInvoke(Nothing, Nothing)
End Sub

Public Sub StartUpdating()
Dim giveFeedBack As New FeedBack(AddressOf IncreaseValue)
oThread = Thread.CurrentThread
For i As Integer = 0 To 15
progy.BeginInvoke(giveFeedBack, New Object() {i})
Thread.Sleep(200)
Next

End Sub

Public Sub IncreaseValue(ByVal myVal As Integer)
progy.Value = myVal
infoLbl.Text = CStr(Math.Round(myVal / progy.Maximum * 100, 2))
End Sub
End Class

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)


Oct 6 '06 #8
No Problem,

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"Patrick Steele" <pa*****@mvps.orgschreef in bericht
news:MP************************@msnews.microsoft.c om...
In article <#9**************@TK2MSFTNGP03.phx.gbl>,
pp*****@nospam.hotmail.com says...
Thanks again, and one more question do you maybe have some good links
for
reading up some more on multi threading and ui or just multi threading.

No, sorry. Nothing bookmarked.

--
Patrick Steele
http://weblogs.asp.net/psteele

Oct 6 '06 #9
Hi Cor, thanks for your explanation. What I'm doing is writing in import
module from foxpro to vb.net, but I can't write entirely on the server-side
because a lot of conversions and a bad table structure in foxpro (no primary
keys), maybe it's possible to write it server-side but it's a lot easier to
write it in vb.net. A user can start this import and it runs for 5 minutes
or so for 20000 records. The problem ofcourse was the ui freezing, so I used
Dim t As New Threading.Thread(AddressOf ImportModule)
t.Start()

and this got me interested in multithreading so I started reading up on it.
I don't actualy plan on using it in this scenario as the method I now use
seems ok to me. Do you see something wrong with it? Would you advise an
other way?

Greetz Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"Cor Ligthert [MVP]" <no************@planet.nlschreef in bericht
news:#U**************@TK2MSFTNGP04.phx.gbl...
Peter,

I have no time to test your code, however the Queue class is in my idea
greath to use for multithreading.

You can use an thrown back event or just a timer in your mainthread to see
if that is filled and empty it while using the synclock. Mostly you see a
lot of in my eyes more dificult solutions.

http://msdn2.microsoft.com/en-us/lib...ons.queue.aspx

http://msdn2.microsoft.com/en-us/library/3a86s51t.aspx

Jon Skeet has written a complete page about Multithreading, however in my
eyes he wants to use multithreading to often, there are very few
situations
where there is no dependency or another reason that using multithreading
has
sense (The only reason I see here often stated is to keep the X (end)
button
alive. In my idea is that the horse behind the cart. You should make your
application faster).

(It has only sense as an offline supplier needs more time and you can
spread
the proces. By instance if you are downloading and you have a speed of
64Kb
and your supplier 20Gb, than multithreading is whithouth sense. In the
oposite way it can make sense if you are downloading from suppliers which
have all 64Kb lines and you have 20Gb. However in Belgium is the avarage
speed of Internet lines so high that this makes probably as well no
sense).
>
http://www.yoda.arachsys.com/csharp/

I hope this gives some ideas,

Cor


"Peter Proost" <pp*****@nospam.hotmail.comschreef in bericht
news:uJ**************@TK2MSFTNGP02.phx.gbl...
Hi group,

I have been doing some reading on threading and updating the ui from a
worker thread and I made this sample which works but I was wondering if
it's
the ok way to do it? Can I improve something? What's the best practise
for
updating multiple controls on my form for example 5 labels or so, just
keep
passing them to the constructor?

Greetz, Peter

On my Form:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim oMyUpd As New MyUpdater(ProgressBar1, lblPct)
oMyUpd.Start()

End Sub

'The worker thread

Imports System.Threading

'================================================= =====================
Public Class MyUpdater
Delegate Sub UpdateStarter()
Delegate Sub FeedBack(ByVal myValue As Integer)

Private oThread As Thread
Private oMyUpdate As New UpdateStarter(AddressOf StartUpdating)

Private progy As ProgressBar
Private infoLbl As Label

Public Sub New(ByVal myProg As ProgressBar, ByVal myLabel As Label)
progy = myProg
infoLbl = myLabel
End Sub

Public Sub [Start]()
oMyUpdate.BeginInvoke(Nothing, Nothing)
End Sub

Public Sub StartUpdating()
Dim giveFeedBack As New FeedBack(AddressOf IncreaseValue)
oThread = Thread.CurrentThread
For i As Integer = 0 To 15
progy.BeginInvoke(giveFeedBack, New Object() {i})
Thread.Sleep(200)
Next

End Sub

Public Sub IncreaseValue(ByVal myVal As Integer)
progy.Value = myVal
infoLbl.Text = CStr(Math.Round(myVal / progy.Maximum * 100, 2))
End Sub
End Class

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to
produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)


Oct 6 '06 #10
Peter,

I am not sure if I have time, but if I have, will try it in the weekend.

Cor

"Peter Proost" <pp*****@nospam.hotmail.comschreef in bericht
news:%2***************@TK2MSFTNGP05.phx.gbl...
Hi Cor, thanks for your explanation. What I'm doing is writing in import
module from foxpro to vb.net, but I can't write entirely on the
server-side
because a lot of conversions and a bad table structure in foxpro (no
primary
keys), maybe it's possible to write it server-side but it's a lot easier
to
write it in vb.net. A user can start this import and it runs for 5 minutes
or so for 20000 records. The problem ofcourse was the ui freezing, so I
used
Dim t As New Threading.Thread(AddressOf ImportModule)
t.Start()

and this got me interested in multithreading so I started reading up on
it.
I don't actualy plan on using it in this scenario as the method I now use
seems ok to me. Do you see something wrong with it? Would you advise an
other way?

Greetz Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"Cor Ligthert [MVP]" <no************@planet.nlschreef in bericht
news:#U**************@TK2MSFTNGP04.phx.gbl...
>Peter,

I have no time to test your code, however the Queue class is in my idea
greath to use for multithreading.

You can use an thrown back event or just a timer in your mainthread to
see
if that is filled and empty it while using the synclock. Mostly you see a
lot of in my eyes more dificult solutions.

http://msdn2.microsoft.com/en-us/lib...ons.queue.aspx

http://msdn2.microsoft.com/en-us/library/3a86s51t.aspx

Jon Skeet has written a complete page about Multithreading, however in my
eyes he wants to use multithreading to often, there are very few
situations
>where there is no dependency or another reason that using multithreading
has
>sense (The only reason I see here often stated is to keep the X (end)
button
>alive. In my idea is that the horse behind the cart. You should make your
application faster).

(It has only sense as an offline supplier needs more time and you can
spread
>the proces. By instance if you are downloading and you have a speed of
64Kb
>and your supplier 20Gb, than multithreading is whithouth sense. In the
oposite way it can make sense if you are downloading from suppliers which
have all 64Kb lines and you have 20Gb. However in Belgium is the avarage
speed of Internet lines so high that this makes probably as well no
sense).
>>
http://www.yoda.arachsys.com/csharp/

I hope this gives some ideas,

Cor


"Peter Proost" <pp*****@nospam.hotmail.comschreef in bericht
news:uJ**************@TK2MSFTNGP02.phx.gbl...
Hi group,

I have been doing some reading on threading and updating the ui from a
worker thread and I made this sample which works but I was wondering if
it's
the ok way to do it? Can I improve something? What's the best practise
for
updating multiple controls on my form for example 5 labels or so, just
keep
passing them to the constructor?

Greetz, Peter

On my Form:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim oMyUpd As New MyUpdater(ProgressBar1, lblPct)
oMyUpd.Start()

End Sub

'The worker thread

Imports System.Threading

'================================================= =====================
Public Class MyUpdater
Delegate Sub UpdateStarter()
Delegate Sub FeedBack(ByVal myValue As Integer)

Private oThread As Thread
Private oMyUpdate As New UpdateStarter(AddressOf StartUpdating)

Private progy As ProgressBar
Private infoLbl As Label

Public Sub New(ByVal myProg As ProgressBar, ByVal myLabel As Label)
progy = myProg
infoLbl = myLabel
End Sub

Public Sub [Start]()
oMyUpdate.BeginInvoke(Nothing, Nothing)
End Sub

Public Sub StartUpdating()
Dim giveFeedBack As New FeedBack(AddressOf IncreaseValue)
oThread = Thread.CurrentThread
For i As Integer = 0 To 15
progy.BeginInvoke(giveFeedBack, New Object() {i})
Thread.Sleep(200)
Next

End Sub

Public Sub IncreaseValue(ByVal myVal As Integer)
progy.Value = myVal
infoLbl.Text = CStr(Math.Round(myVal / progy.Maximum * 100, 2))
End Sub
End Class

--
Programming today is a race between software engineers striving to
build
bigger and better idiot-proof programs, and the Universe trying to
produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)




Oct 7 '06 #11

Peter Proost wrote:
Thanks again, and one more question do you maybe have some good links for
reading up some more on multi threading and ui or just multi threading.

Greetz, Peter
Peter:

http://msdn.microsoft.com/library/de...ms01232003.asp

This is part one of a 3 part article on the subject of interacting with
a UI from a background thread. The code is in C#, but the problem and
solutions are the same for VB.NET. Also, keep in mind that in VB 2005,
these simple scenarios are made easy by the addition of the
BackgroundWorker component.

--
Tom Shelton

Oct 7 '06 #12
Response send in Dutch by email.

Cor

"Peter Proost" <pp*****@nospam.hotmail.comschreef in bericht
news:%2***************@TK2MSFTNGP05.phx.gbl...
Hi Cor, thanks for your explanation. What I'm doing is writing in import
module from foxpro to vb.net, but I can't write entirely on the
server-side
because a lot of conversions and a bad table structure in foxpro (no
primary
keys), maybe it's possible to write it server-side but it's a lot easier
to
write it in vb.net. A user can start this import and it runs for 5 minutes
or so for 20000 records. The problem ofcourse was the ui freezing, so I
used
Dim t As New Threading.Thread(AddressOf ImportModule)
t.Start()

and this got me interested in multithreading so I started reading up on
it.
I don't actualy plan on using it in this scenario as the method I now use
seems ok to me. Do you see something wrong with it? Would you advise an
other way?

Greetz Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"Cor Ligthert [MVP]" <no************@planet.nlschreef in bericht
news:#U**************@TK2MSFTNGP04.phx.gbl...
>Peter,

I have no time to test your code, however the Queue class is in my idea
greath to use for multithreading.

You can use an thrown back event or just a timer in your mainthread to
see
if that is filled and empty it while using the synclock. Mostly you see a
lot of in my eyes more dificult solutions.

http://msdn2.microsoft.com/en-us/lib...ons.queue.aspx

http://msdn2.microsoft.com/en-us/library/3a86s51t.aspx

Jon Skeet has written a complete page about Multithreading, however in my
eyes he wants to use multithreading to often, there are very few
situations
>where there is no dependency or another reason that using multithreading
has
>sense (The only reason I see here often stated is to keep the X (end)
button
>alive. In my idea is that the horse behind the cart. You should make your
application faster).

(It has only sense as an offline supplier needs more time and you can
spread
>the proces. By instance if you are downloading and you have a speed of
64Kb
>and your supplier 20Gb, than multithreading is whithouth sense. In the
oposite way it can make sense if you are downloading from suppliers which
have all 64Kb lines and you have 20Gb. However in Belgium is the avarage
speed of Internet lines so high that this makes probably as well no
sense).
>>
http://www.yoda.arachsys.com/csharp/

I hope this gives some ideas,

Cor


"Peter Proost" <pp*****@nospam.hotmail.comschreef in bericht
news:uJ**************@TK2MSFTNGP02.phx.gbl...
Hi group,

I have been doing some reading on threading and updating the ui from a
worker thread and I made this sample which works but I was wondering if
it's
the ok way to do it? Can I improve something? What's the best practise
for
updating multiple controls on my form for example 5 labels or so, just
keep
passing them to the constructor?

Greetz, Peter

On my Form:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim oMyUpd As New MyUpdater(ProgressBar1, lblPct)
oMyUpd.Start()

End Sub

'The worker thread

Imports System.Threading

'================================================= =====================
Public Class MyUpdater
Delegate Sub UpdateStarter()
Delegate Sub FeedBack(ByVal myValue As Integer)

Private oThread As Thread
Private oMyUpdate As New UpdateStarter(AddressOf StartUpdating)

Private progy As ProgressBar
Private infoLbl As Label

Public Sub New(ByVal myProg As ProgressBar, ByVal myLabel As Label)
progy = myProg
infoLbl = myLabel
End Sub

Public Sub [Start]()
oMyUpdate.BeginInvoke(Nothing, Nothing)
End Sub

Public Sub StartUpdating()
Dim giveFeedBack As New FeedBack(AddressOf IncreaseValue)
oThread = Thread.CurrentThread
For i As Integer = 0 To 15
progy.BeginInvoke(giveFeedBack, New Object() {i})
Thread.Sleep(200)
Next

End Sub

Public Sub IncreaseValue(ByVal myVal As Integer)
progy.Value = myVal
infoLbl.Text = CStr(Math.Round(myVal / progy.Maximum * 100, 2))
End Sub
End Class

--
Programming today is a race between software engineers striving to
build
bigger and better idiot-proof programs, and the Universe trying to
produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)




Oct 8 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: Charles Law | last post by:
Hi guys. I'm back on the threading gig again. It's the age-old question about waiting for something to happen without wasting time doing it. Take two threads: the main thread and a worker...
5
by: Claire | last post by:
My progress window is created by a secondary thread and then updated by it while a file is uploaded. There's an avi animation control on there that should show the move file avi. Plus a progress...
2
by: BG | last post by:
We're having trouble writing the code to update a UI control (label.Text) from a secondary thread. We're using C# with Windows Forms. We have a main form named MainForm, a splash screen form...
2
by: Don Tucker | last post by:
Hello, I am using Visual Studio 2005 .Net, coding in C#. I am working through the threading walkthrough: ...
9
by: Jervin Justin | last post by:
Hi, I've been having this problem for some time with Web Forms: I have a web app that sends data to a service when the user presses a button. Based on the data, the server will send several...
5
by: Mark R. Dawson | last post by:
Hi all, I may be missing something with how databinding works but I have bound a datasource to a control and everything is great, the control updates to reflect the state of my datasource when I...
5
by: Alan T | last post by:
I will do several things in my thread: Copy a file to a location Update database record Read the file content Write the content to a log file If I call Thread.Abort(), it may be possible to...
8
by: =?Utf-8?B?R3JlZyBMYXJzZW4=?= | last post by:
I'm trying to figure out how to modify a panel (panel1) from a backgroundworker thread. But can't get the panel to show the new controls added by the backgroundwork task. Here is my code. In...
20
by: cty0000 | last post by:
I have some question.. This is my first time to use thread.. Following code does not have error but two warring The warring is Warning 2 'System.Threading.Thread.Suspend()' is obsolete:...
5
by: P.J.M. Beker | last post by:
Hi there, I'm currently writing a program in which I use the FileMonitor to monitor a folder in which I store downloaded images. I know that I can't add much coding in the filemonitor's event in...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.