Can someone tell me why my program hangs when I call a timer function from
within a Do Loop. Here is the code:
Public Class MainForm
Public intCounter As Double = 0
Public intMin As Double
Public intSec As Double
Private Sub btnStart_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnStart.Click
If btnStart.Text = "Start" Then
btnStart.Text = "Stop"
Else
btnStart.Text = "Start"
End If
lblTimeMin.Text = "0:"
lblTimeSec.Text = "00"
'Do While intCounter < 600
Call MyTimer()
intCounter = intCounter + 1
intMin = Int(intCounter / 60)
intSec = intCounter Mod 60
lblTimeMin.Text = Convert.ToString(intMin & ":")
lblTimeSec.Text = Convert.ToString(Format(intSec, "00"))
'Loop
End Sub
Private Sub MyTimer()
Dim start, finish As Double
start = Microsoft.VisualBasic.DateAndTime.Timer
finish = start + 1
Do While Microsoft.VisualBasic.DateAndTime.Timer < finish
Loop
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
Thanks,
Mike 12 1573
Hello MikeS,
can you debug and tell if it is hanging in the Timer call ?
Private Sub MyTimer()
Dim start, finish As Double
start = Microsoft.VisualBasic.DateAndTime.Timer
finish = start + 1
Do While Microsoft.VisualBasic.DateAndTime.Timer < finish
Loop
Hello Wolf,
The timer will work without the Do Loop and the Do Loop will work without
the timer. They will NOT work together.
"Wolf Saenger" wrote:
Hello MikeS,
can you debug and tell if it is hanging in the Timer call ?
Private Sub MyTimer()
Dim start, finish As Double
start = Microsoft.VisualBasic.DateAndTime.Timer
finish = start + 1
Do While Microsoft.VisualBasic.DateAndTime.Timer < finish
Loop
Hello MikeS,
Hello Wolf,
The timer will work without the Do Loop and the Do Loop will work
without the timer. They will NOT work together.
"Wolf Saenger" wrote:
>Hello MikeS,
can you debug and tell if it is hanging in the Timer call ?
>>Private Sub MyTimer() Dim start, finish As Double start = Microsoft.VisualBasic.DateAndTime.Timer finish = start + 1 Do While Microsoft.VisualBasic.DateAndTime.Timer < finish Loop
So than you what you inside the loop?
do you set the time again?
Mike
Of course the answer from Armin obout using a timer is better however just
for the situation itself.
Can you try this one?
Do While start < finish
start = Microsoft.VisualBasic.DateAndTime.Timer
Cor
Your program hangs because of the loop and because of the fact that
everything needs to be processed by the GUI thread
if you would throw in an refresh on the labels you wil see that it actually
is working
however as soon as you move the form it will freeze
this is normall behavior this is just how things work the message pump is so
bussy with processing that it can`t find the time to redraw the form
how can you bypass this problem ?
well the simplest way in my opinion is to split the worker thread from the
GUI my personal favorite to do such simple tasks is to use asynchronous
delegates
here is a modified version of your code that will work how you wanted it to
Public Class MainForm
Public intCounter As Double
Public intMin As Double
Public intSec As Double
Private mvar_WorkerRun As Boolean
Private Sub btnStart_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnstart.Click
If btnstart.Text = "Start" Then
btnstart.Text = "Stop"
mvar_WorkerRun = False
Else
btnstart.Text = "Start"
lblTimeMin.Text = "0:"
lblTimeSec.Text = "00"
mvar_WorkerRun = True
Dim AsyncVerwerken As New AsynChWiNoParameters(AddressOf
Me.Worker)
AsyncVerwerken.BeginInvoke(Nothing, Nothing)
End If
End Sub
Delegate Sub AsynChWiNoParameters()
Private Sub Worker()
Do While intCounter < 600
Call MyTimer()
intCounter = intCounter + 1
intMin = Int(intCounter / 60)
intSec = intCounter Mod 60
GUIUpdate()
If Not mvar_WorkerRun Then Exit Do
Loop
End Sub
Private Sub GUIUpdate()
If lblTimeMin.InvokeRequired Then
Dim d As New AsynChWiNoParameters(AddressOf GUIUpdate)
Me.Invoke(d, New Object() {})
Else
lblTimeMin.Text = Convert.ToString(intMin & ":")
lblTimeSec.Text = Convert.ToString(Format(intSec, "00"))
End If
End Sub
Private Sub MyTimer()
Dim start, finish As Double
start = Microsoft.VisualBasic.DateAndTime.Timer
finish = start + 1
Do While Microsoft.VisualBasic.DateAndTime.Timer < finish OrElse Not
mvar_WorkerRun
Loop
End Sub
Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Me.mvar_WorkerRun = False
End Sub
End Class
HTH
Michel
"MikeS" wrote:
Can someone tell me why my program hangs when I call a timer function from
within a Do Loop. Here is the code:
Public Class MainForm
Public intCounter As Double = 0
Public intMin As Double
Public intSec As Double
Private Sub btnStart_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnStart.Click
If btnStart.Text = "Start" Then
btnStart.Text = "Stop"
Else
btnStart.Text = "Start"
End If
lblTimeMin.Text = "0:"
lblTimeSec.Text = "00"
'Do While intCounter < 600
Call MyTimer()
intCounter = intCounter + 1
intMin = Int(intCounter / 60)
intSec = intCounter Mod 60
lblTimeMin.Text = Convert.ToString(intMin & ":")
lblTimeSec.Text = Convert.ToString(Format(intSec, "00"))
'Loop
End Sub
Private Sub MyTimer()
Dim start, finish As Double
start = Microsoft.VisualBasic.DateAndTime.Timer
finish = start + 1
Do While Microsoft.VisualBasic.DateAndTime.Timer < finish
Loop
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
Thanks,
Mike
Ad.
For those who not understand Dutch
AsyncVerwerken
Can be by instance translated as
AsyncProcess
or
AsyncRun
or
AsyncPerform
Cor
"Michel Posseth [MCP]" <Mi**************@discussions.microsoft.comschre ef
in bericht news:75**********************************@microsof t.com...
Your program hangs because of the loop and because of the fact that
everything needs to be processed by the GUI thread
if you would throw in an refresh on the labels you wil see that it
actually
is working
however as soon as you move the form it will freeze
this is normall behavior this is just how things work the message pump is
so
bussy with processing that it can`t find the time to redraw the form
how can you bypass this problem ?
well the simplest way in my opinion is to split the worker thread from the
GUI my personal favorite to do such simple tasks is to use asynchronous
delegates
here is a modified version of your code that will work how you wanted it
to
Public Class MainForm
Public intCounter As Double
Public intMin As Double
Public intSec As Double
Private mvar_WorkerRun As Boolean
Private Sub btnStart_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnstart.Click
If btnstart.Text = "Start" Then
btnstart.Text = "Stop"
mvar_WorkerRun = False
Else
btnstart.Text = "Start"
lblTimeMin.Text = "0:"
lblTimeSec.Text = "00"
mvar_WorkerRun = True
Dim AsyncVerwerken As New AsynChWiNoParameters(AddressOf
Me.Worker)
AsyncVerwerken.BeginInvoke(Nothing, Nothing)
End If
End Sub
Delegate Sub AsynChWiNoParameters()
Private Sub Worker()
Do While intCounter < 600
Call MyTimer()
intCounter = intCounter + 1
intMin = Int(intCounter / 60)
intSec = intCounter Mod 60
GUIUpdate()
If Not mvar_WorkerRun Then Exit Do
Loop
End Sub
Private Sub GUIUpdate()
If lblTimeMin.InvokeRequired Then
Dim d As New AsynChWiNoParameters(AddressOf GUIUpdate)
Me.Invoke(d, New Object() {})
Else
lblTimeMin.Text = Convert.ToString(intMin & ":")
lblTimeSec.Text = Convert.ToString(Format(intSec, "00"))
End If
End Sub
Private Sub MyTimer()
Dim start, finish As Double
start = Microsoft.VisualBasic.DateAndTime.Timer
finish = start + 1
Do While Microsoft.VisualBasic.DateAndTime.Timer < finish OrElse
Not
mvar_WorkerRun
Loop
End Sub
Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Me.mvar_WorkerRun = False
End Sub
End Class
HTH
Michel
"MikeS" wrote:
>Can someone tell me why my program hangs when I call a timer function from within a Do Loop. Here is the code:
Public Class MainForm
Public intCounter As Double = 0 Public intMin As Double Public intSec As Double
Private Sub btnStart_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnStart.Click
If btnStart.Text = "Start" Then btnStart.Text = "Stop" Else btnStart.Text = "Start" End If
lblTimeMin.Text = "0:" lblTimeSec.Text = "00"
'Do While intCounter < 600 Call MyTimer() intCounter = intCounter + 1 intMin = Int(intCounter / 60) intSec = intCounter Mod 60
lblTimeMin.Text = Convert.ToString(intMin & ":") lblTimeSec.Text = Convert.ToString(Format(intSec, "00")) 'Loop
End Sub
Private Sub MyTimer() Dim start, finish As Double start = Microsoft.VisualBasic.DateAndTime.Timer finish = start + 1 Do While Microsoft.VisualBasic.DateAndTime.Timer < finish
Loop
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnExit.Click Me.Close() End Sub End Class
Thanks,
Mike
>
For those who not understand Dutch
AsyncVerwerken
Can be by instance translated as
AsyncProcess
or
AsyncRun
or
AsyncPerform
I should have added my friendly smile while writing this.
Nice help of course from Michel
:-)
Cor
>>AsyncVerwerken
Normally i do all my coding in English ,,, funny that this slipped in
and for this
I should have added my friendly smile while writing this.
i would never doubt on your intentions , i know you well enough ;--)
michel
"Cor Ligthert[MVP]" wrote:
For those who not understand Dutch
AsyncVerwerken
Can be by instance translated as
AsyncProcess
or
AsyncRun
or
AsyncPerform
I should have added my friendly smile while writing this.
Nice help of course from Michel
:-)
Cor
Thanks to all for your input. That works really good. One thing I noticed
though, is if you stop and restart the timer, it starts doing double time.
"Michel Posseth [MCP]" wrote:
Your program hangs because of the loop and because of the fact that
everything needs to be processed by the GUI thread
if you would throw in an refresh on the labels you wil see that it actually
is working
however as soon as you move the form it will freeze
this is normall behavior this is just how things work the message pump is so
bussy with processing that it can`t find the time to redraw the form
how can you bypass this problem ?
well the simplest way in my opinion is to split the worker thread from the
GUI my personal favorite to do such simple tasks is to use asynchronous
delegates
here is a modified version of your code that will work how you wanted it to
Public Class MainForm
Public intCounter As Double
Public intMin As Double
Public intSec As Double
Private mvar_WorkerRun As Boolean
Private Sub btnStart_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnstart.Click
If btnstart.Text = "Start" Then
btnstart.Text = "Stop"
mvar_WorkerRun = False
Else
btnstart.Text = "Start"
lblTimeMin.Text = "0:"
lblTimeSec.Text = "00"
mvar_WorkerRun = True
Dim AsyncVerwerken As New AsynChWiNoParameters(AddressOf
Me.Worker)
AsyncVerwerken.BeginInvoke(Nothing, Nothing)
End If
End Sub
Delegate Sub AsynChWiNoParameters()
Private Sub Worker()
Do While intCounter < 600
Call MyTimer()
intCounter = intCounter + 1
intMin = Int(intCounter / 60)
intSec = intCounter Mod 60
GUIUpdate()
If Not mvar_WorkerRun Then Exit Do
Loop
End Sub
Private Sub GUIUpdate()
If lblTimeMin.InvokeRequired Then
Dim d As New AsynChWiNoParameters(AddressOf GUIUpdate)
Me.Invoke(d, New Object() {})
Else
lblTimeMin.Text = Convert.ToString(intMin & ":")
lblTimeSec.Text = Convert.ToString(Format(intSec, "00"))
End If
End Sub
Private Sub MyTimer()
Dim start, finish As Double
start = Microsoft.VisualBasic.DateAndTime.Timer
finish = start + 1
Do While Microsoft.VisualBasic.DateAndTime.Timer < finish OrElse Not
mvar_WorkerRun
Loop
End Sub
Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Me.mvar_WorkerRun = False
End Sub
End Class
HTH
Michel
"MikeS" wrote:
Can someone tell me why my program hangs when I call a timer function from
within a Do Loop. Here is the code:
Public Class MainForm
Public intCounter As Double = 0
Public intMin As Double
Public intSec As Double
Private Sub btnStart_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnStart.Click
If btnStart.Text = "Start" Then
btnStart.Text = "Stop"
Else
btnStart.Text = "Start"
End If
lblTimeMin.Text = "0:"
lblTimeSec.Text = "00"
'Do While intCounter < 600
Call MyTimer()
intCounter = intCounter + 1
intMin = Int(intCounter / 60)
intSec = intCounter Mod 60
lblTimeMin.Text = Convert.ToString(intMin & ":")
lblTimeSec.Text = Convert.ToString(Format(intSec, "00"))
'Loop
End Sub
Private Sub MyTimer()
Dim start, finish As Double
start = Microsoft.VisualBasic.DateAndTime.Timer
finish = start + 1
Do While Microsoft.VisualBasic.DateAndTime.Timer < finish
Loop
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
Thanks,
Mike
Thanks to all for your input. That works really good. One thing I
noticed
though, is if you stop and restart the timer, it starts doing double time.
this would solve your problem
Private Sub Worker()
intMin =0: intSec =0
However what would happen if your worker loop finishes its 600 iterations ??
so in advance bugfix 2
btnstart.Text = "Stop"
mvar_WorkerRun = False
End Sub
so the whole Worker sub becomes
Private Sub Worker()
intMin =0: intSec =0
Do While intCounter < 600
Call MyTimer()
intCounter = intCounter + 1
intMin = Int(intCounter / 60)
intSec = intCounter Mod 60
GUIUpdate()
If Not mvar_WorkerRun Then Exit Do
Loop
btnstart.Text = "Stop"
mvar_WorkerRun = False
End Sub
HTH
Michel
"MikeS" <Mi***@discussions.microsoft.comschreef in bericht
news:41**********************************@microsof t.com...
Thanks to all for your input. That works really good. One thing I
noticed
though, is if you stop and restart the timer, it starts doing double time.
"Michel Posseth [MCP]" wrote:
>Your program hangs because of the loop and because of the fact that everything needs to be processed by the GUI thread
if you would throw in an refresh on the labels you wil see that it actually is working however as soon as you move the form it will freeze
this is normall behavior this is just how things work the message pump is so bussy with processing that it can`t find the time to redraw the form
how can you bypass this problem ?
well the simplest way in my opinion is to split the worker thread from the GUI my personal favorite to do such simple tasks is to use asynchronous delegates
here is a modified version of your code that will work how you wanted it to
Public Class MainForm Public intCounter As Double Public intMin As Double Public intSec As Double Private mvar_WorkerRun As Boolean Private Sub btnStart_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnstart.Click
If btnstart.Text = "Start" Then btnstart.Text = "Stop" mvar_WorkerRun = False Else btnstart.Text = "Start" lblTimeMin.Text = "0:" lblTimeSec.Text = "00" mvar_WorkerRun = True Dim AsyncVerwerken As New AsynChWiNoParameters(AddressOf Me.Worker) AsyncVerwerken.BeginInvoke(Nothing, Nothing) End If End Sub Delegate Sub AsynChWiNoParameters()
Private Sub Worker() Do While intCounter < 600 Call MyTimer() intCounter = intCounter + 1 intMin = Int(intCounter / 60) intSec = intCounter Mod 60 GUIUpdate() If Not mvar_WorkerRun Then Exit Do Loop End Sub Private Sub GUIUpdate() If lblTimeMin.InvokeRequired Then Dim d As New AsynChWiNoParameters(AddressOf GUIUpdate) Me.Invoke(d, New Object() {}) Else lblTimeMin.Text = Convert.ToString(intMin & ":") lblTimeSec.Text = Convert.ToString(Format(intSec, "00")) End If
End Sub Private Sub MyTimer() Dim start, finish As Double start = Microsoft.VisualBasic.DateAndTime.Timer finish = start + 1 Do While Microsoft.VisualBasic.DateAndTime.Timer < finish OrElse Not mvar_WorkerRun
Loop End Sub Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Me.mvar_WorkerRun = False End Sub End Class
HTH
Michel
"MikeS" wrote:
Can someone tell me why my program hangs when I call a timer function
from
within a Do Loop. Here is the code:
Public Class MainForm
Public intCounter As Double = 0
Public intMin As Double
Public intSec As Double
Private Sub btnStart_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnStart.Click
If btnStart.Text = "Start" Then
btnStart.Text = "Stop"
Else
btnStart.Text = "Start"
End If
lblTimeMin.Text = "0:"
lblTimeSec.Text = "00"
'Do While intCounter < 600
Call MyTimer()
intCounter = intCounter + 1
intMin = Int(intCounter / 60)
intSec = intCounter Mod 60
lblTimeMin.Text = Convert.ToString(intMin & ":")
lblTimeSec.Text = Convert.ToString(Format(intSec, "00"))
'Loop
End Sub
Private Sub MyTimer()
Dim start, finish As Double
start = Microsoft.VisualBasic.DateAndTime.Timer
finish = start + 1
Do While Microsoft.VisualBasic.DateAndTime.Timer < finish
Loop
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
Thanks,
Mike
Michel,
I tried that but it didn't resolve the double time issue. Just to give you
an idea, imagine the timing of this sequence:
---1---2---3---4---5---6---7---8 the first time and after stopping and
restarting
--12--34--56--78 and so on.
Hope this makes sense.
Thanks,
Mike
"Michel Posseth [MCP]" wrote:
>
Thanks to all for your input. That works really good. One thing I
noticed
though, is if you stop and restart the timer, it starts doing double time.
this would solve your problem
Private Sub Worker()
intMin =0: intSec =0
However what would happen if your worker loop finishes its 600 iterations ??
so in advance bugfix 2
btnstart.Text = "Stop"
mvar_WorkerRun = False
End Sub
so the whole Worker sub becomes
Private Sub Worker()
intMin =0: intSec =0
Do While intCounter < 600
Call MyTimer()
intCounter = intCounter + 1
intMin = Int(intCounter / 60)
intSec = intCounter Mod 60
GUIUpdate()
If Not mvar_WorkerRun Then Exit Do
Loop
btnstart.Text = "Stop"
mvar_WorkerRun = False
End Sub
HTH
Michel
"MikeS" <Mi***@discussions.microsoft.comschreef in bericht
news:41**********************************@microsof t.com...
Thanks to all for your input. That works really good. One thing I
noticed
though, is if you stop and restart the timer, it starts doing double time.
"Michel Posseth [MCP]" wrote:
Your program hangs because of the loop and because of the fact that
everything needs to be processed by the GUI thread
if you would throw in an refresh on the labels you wil see that it
actually
is working
however as soon as you move the form it will freeze
this is normall behavior this is just how things work the message pump is
so
bussy with processing that it can`t find the time to redraw the form
how can you bypass this problem ?
well the simplest way in my opinion is to split the worker thread from
the
GUI my personal favorite to do such simple tasks is to use asynchronous
delegates
here is a modified version of your code that will work how you wanted it
to
Public Class MainForm
Public intCounter As Double
Public intMin As Double
Public intSec As Double
Private mvar_WorkerRun As Boolean
Private Sub btnStart_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnstart.Click
If btnstart.Text = "Start" Then
btnstart.Text = "Stop"
mvar_WorkerRun = False
Else
btnstart.Text = "Start"
lblTimeMin.Text = "0:"
lblTimeSec.Text = "00"
mvar_WorkerRun = True
Dim AsyncVerwerken As New AsynChWiNoParameters(AddressOf
Me.Worker)
AsyncVerwerken.BeginInvoke(Nothing, Nothing)
End If
End Sub
Delegate Sub AsynChWiNoParameters()
Private Sub Worker()
Do While intCounter < 600
Call MyTimer()
intCounter = intCounter + 1
intMin = Int(intCounter / 60)
intSec = intCounter Mod 60
GUIUpdate()
If Not mvar_WorkerRun Then Exit Do
Loop
End Sub
Private Sub GUIUpdate()
If lblTimeMin.InvokeRequired Then
Dim d As New AsynChWiNoParameters(AddressOf GUIUpdate)
Me.Invoke(d, New Object() {})
Else
lblTimeMin.Text = Convert.ToString(intMin & ":")
lblTimeSec.Text = Convert.ToString(Format(intSec, "00"))
End If
End Sub
Private Sub MyTimer()
Dim start, finish As Double
start = Microsoft.VisualBasic.DateAndTime.Timer
finish = start + 1
Do While Microsoft.VisualBasic.DateAndTime.Timer < finish OrElse
Not
mvar_WorkerRun
Loop
End Sub
Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Me.mvar_WorkerRun = False
End Sub
End Class
HTH
Michel
"MikeS" wrote:
Can someone tell me why my program hangs when I call a timer function
from
within a Do Loop. Here is the code:
Public Class MainForm
Public intCounter As Double = 0
Public intMin As Double
Public intSec As Double
Private Sub btnStart_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnStart.Click
If btnStart.Text = "Start" Then
btnStart.Text = "Stop"
Else
btnStart.Text = "Start"
End If
lblTimeMin.Text = "0:"
lblTimeSec.Text = "00"
'Do While intCounter < 600
Call MyTimer()
intCounter = intCounter + 1
intMin = Int(intCounter / 60)
intSec = intCounter Mod 60
lblTimeMin.Text = Convert.ToString(intMin & ":")
lblTimeSec.Text = Convert.ToString(Format(intSec, "00"))
'Loop
End Sub
Private Sub MyTimer()
Dim start, finish As Double
start = Microsoft.VisualBasic.DateAndTime.Timer
finish = start + 1
Do While Microsoft.VisualBasic.DateAndTime.Timer < finish
Loop
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
Thanks,
Mike
AHA
i see what you mean
This version wil also not hog up the CPU
Public Class MainForm
Public intCounter As Double
Public intMin As Double
Public intSec As Double
Private mvar_WorkerRun As Boolean
Private Sub btnStart_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnstart.Click
mvar_WorkerRun = Not mvar_WorkerRun
If mvar_WorkerRun Then
btnstart.Text = "Stop"
lbltimemin.Text = "0:"
lbltimesec.Text = "00"
Dim AsyncVerwerken As New AsynChWiNoParameters(AddressOf Me.Worker)
AsyncVerwerken.BeginInvoke(Nothing, Nothing)
Else
btnstart.Text = "Start"
End If
End Sub
Delegate Sub AsynChWiNoParameters()
Private Sub Worker()
intCounter = 0
Do While intCounter < 600
Call MyTimer()
intCounter = intCounter + 1
intMin = Int(intCounter / 60)
intSec = intCounter Mod 60
GUIUpdate()
If mvar_WorkerRun Then
Application.DoEvents()
Else
Exit Do
End If
Loop
End Sub
Private Sub GUIUpdate()
If lblTimeMin.InvokeRequired Then
Dim d As New AsynChWiNoParameters(AddressOf GUIUpdate)
Me.Invoke(d, New Object() {})
Else
lblTimeMin.Text = Convert.ToString(intMin & ":")
lblTimeSec.Text = Convert.ToString(Format(intSec, "00"))
End If
End Sub
Private Sub MyTimer()
Dim start, finish As Double
start = Microsoft.VisualBasic.DateAndTime.Timer
finish = start + 1
Do While Microsoft.VisualBasic.DateAndTime.Timer < finish
If mvar_WorkerRun Then
Application.DoEvents()
Else
Exit Do
End If
Loop
End Sub
Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Me.mvar_WorkerRun = False
End Sub
End Class
"MikeS" <Mi***@discussions.microsoft.comschreef in bericht
news:49**********************************@microsof t.com...
Michel,
I tried that but it didn't resolve the double time issue. Just to give
you
an idea, imagine the timing of this sequence:
---1---2---3---4---5---6---7---8 the first time and after stopping and
restarting
--12--34--56--78 and so on.
Hope this makes sense.
Thanks,
Mike
"Michel Posseth [MCP]" wrote:
>>
Thanks to all for your input. That works really good. One thing I
noticed
though, is if you stop and restart the timer, it starts doing double
time.
this would solve your problem Private Sub Worker() intMin =0: intSec =0
However what would happen if your worker loop finishes its 600 iterations ?? so in advance bugfix 2
btnstart.Text = "Stop" mvar_WorkerRun = False End Sub
so the whole Worker sub becomes
Private Sub Worker() intMin =0: intSec =0 Do While intCounter < 600 Call MyTimer() intCounter = intCounter + 1 intMin = Int(intCounter / 60) intSec = intCounter Mod 60 GUIUpdate() If Not mvar_WorkerRun Then Exit Do Loop btnstart.Text = "Stop" mvar_WorkerRun = False End Sub
HTH
Michel
"MikeS" <Mi***@discussions.microsoft.comschreef in bericht news:41**********************************@microso ft.com...
Thanks to all for your input. That works really good. One thing I
noticed
though, is if you stop and restart the timer, it starts doing double
time.
"Michel Posseth [MCP]" wrote:
Your program hangs because of the loop and because of the fact that everything needs to be processed by the GUI thread
if you would throw in an refresh on the labels you wil see that it actually is working however as soon as you move the form it will freeze
this is normall behavior this is just how things work the message pump is so bussy with processing that it can`t find the time to redraw the form
how can you bypass this problem ?
well the simplest way in my opinion is to split the worker thread from the GUI my personal favorite to do such simple tasks is to use asynchronous delegates
here is a modified version of your code that will work how you wanted it to
Public Class MainForm Public intCounter As Double Public intMin As Double Public intSec As Double Private mvar_WorkerRun As Boolean Private Sub btnStart_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnstart.Click
If btnstart.Text = "Start" Then btnstart.Text = "Stop" mvar_WorkerRun = False Else btnstart.Text = "Start" lblTimeMin.Text = "0:" lblTimeSec.Text = "00" mvar_WorkerRun = True Dim AsyncVerwerken As New AsynChWiNoParameters(AddressOf Me.Worker) AsyncVerwerken.BeginInvoke(Nothing, Nothing) End If End Sub Delegate Sub AsynChWiNoParameters()
Private Sub Worker() Do While intCounter < 600 Call MyTimer() intCounter = intCounter + 1 intMin = Int(intCounter / 60) intSec = intCounter Mod 60 GUIUpdate() If Not mvar_WorkerRun Then Exit Do Loop End Sub Private Sub GUIUpdate() If lblTimeMin.InvokeRequired Then Dim d As New AsynChWiNoParameters(AddressOf GUIUpdate) Me.Invoke(d, New Object() {}) Else lblTimeMin.Text = Convert.ToString(intMin & ":") lblTimeSec.Text = Convert.ToString(Format(intSec, "00")) End If
End Sub Private Sub MyTimer() Dim start, finish As Double start = Microsoft.VisualBasic.DateAndTime.Timer finish = start + 1 Do While Microsoft.VisualBasic.DateAndTime.Timer < finish OrElse Not mvar_WorkerRun
Loop End Sub Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Me.mvar_WorkerRun = False End Sub End Class
HTH
Michel
"MikeS" wrote:
Can someone tell me why my program hangs when I call a timer
function
from
within a Do Loop. Here is the code:
Public Class MainForm
Public intCounter As Double = 0
Public intMin As Double
Public intSec As Double
Private Sub btnStart_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnStart.Click
If btnStart.Text = "Start" Then
btnStart.Text = "Stop"
Else
btnStart.Text = "Start"
End If
lblTimeMin.Text = "0:"
lblTimeSec.Text = "00"
'Do While intCounter < 600
Call MyTimer()
intCounter = intCounter + 1
intMin = Int(intCounter / 60)
intSec = intCounter Mod 60
lblTimeMin.Text = Convert.ToString(intMin & ":")
lblTimeSec.Text = Convert.ToString(Format(intSec, "00"))
'Loop
End Sub
Private Sub MyTimer()
Dim start, finish As Double
start = Microsoft.VisualBasic.DateAndTime.Timer
finish = start + 1
Do While Microsoft.VisualBasic.DateAndTime.Timer < finish
Loop
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
Thanks,
Mike
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: GDumencu |
last post by:
I have a C# program that have to run all the time. For
some reasons I cannot make it a service.
This program has 3 timers and by time to time ( days or
weeks) one of them stops. Some times, starts...
|
by: Dmitry Demchuk |
last post by:
Hi everybody.
Recently I ran into situation with System.Threading.Timer in my ASP.NET
application. I reinstalled Windows on one of my servers and got timers stop
firing events after while, they...
|
by: Cider123 |
last post by:
I was originally using:
System.Windows.Forms.Timer
It started to lock my Window Service up, so I went to the next
evolution:
System.Threading.Timer
All was good. I was using it to send...
|
by: Nathan Kovac |
last post by:
I have a feeling I am missing something simple, but I just can't find it.
Perhaps someone can give me a lead on where to look. I will describe the
issue then post my code to the web service. My...
|
by: Jeff Greenland |
last post by:
Hello everyone,
I am having problems with Timers in a web application. They just seem to
stop running after 15 minutes or so.
My web application is set up like this:
When a user hits a...
|
by: Mark Rae |
last post by:
Hi,
I've seen several articles about using System Timers in ASP.NET solutions,
specifically setting them up in Global.asax' Application_OnStart event.
I'm thinking about the scenario where I...
|
by: WhiteSocksGuy |
last post by:
Help! I am new to Visual Basic .Net (version 2002) and I am trying to get a
System.Timers.Timer to work for me to display a splash screen for about two
seconds and then load the main form. I have...
|
by: Michael C# |
last post by:
Hi all,
I set up a System.Timers.Time in my app. The code basically just updates
the screen, but since the processing performed is so CPU-intensive, I wanted
to make sure it gets updated...
|
by: |
last post by:
Frustrated.. (I have seen other posts regarding this problem with no
resolution..) I am using dotnet 1.1 with latest SP on a Win2KP box (actually
2 boxes), have even run the service on WinXP SP2...
|
by: Jonathan Woods |
last post by:
Hi there,
I have three methods these need to execute at every interval time. I
would like to know which option is better?
Option A)
Three System.Timers.Timer objects these execute each...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |