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) 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
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
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
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
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
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
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)
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
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)
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)
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
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)
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
by: Don Tucker |
last post by:
Hello, I am using Visual Studio 2005 .Net, coding in C#. I am working
through the threading walkthrough:
...
|
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...
|
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...
|
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...
|
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...
|
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:...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| | |