timer control

  • Thread starter Thread starter Ratnesh Raval
  • Start date Start date
R

Ratnesh Raval

hi all,

i m having some problem in timer control.

sub timer.tick()
timer.stop()
do...something
timer.enabled = true
end sub

this works fine. but after timer.stop() when i try to enable it from some
other functions it doesnt work. and the timer stops forever

thnx
 
Hello Ratnesh,
sub timer.tick()
timer.stop()
do...something
timer.enabled = true
end sub

After calling timer.Stop(), you need to call timer.Start() to restart the
timer.
 
thanks jared,

but that not working either. i've tried both ways, even both together. but
no luck

Jared Parsons said:
Hello Ratnesh,
sub timer.tick()
timer.stop()
do...something
timer.enabled = true
end sub

After calling timer.Stop(), you need to call timer.Start() to restart the
timer.
--
Jared Parsons [MSFT]
(e-mail address removed)
All opinions are my own. All content is provided "AS IS" with no
warranties, and confers no rights.
 
Hello Ratnesh,
thanks jared,

but that not working either. i've tried both ways, even both together.
but no luck

What happens when you call Start( exception, or just nothing). Try toggling
the Enabled and not calling Stop() at all.
 
when i call start or enabled=true. nothing happens in any case. but the tick
event doesnt fire after the interval.
i've also tried to toggle the enabled property and didnt use Stop()

still no luck. i dont know whats wrong.
its like this

sub timer.tick()
timer.stop() [or timer.enabled=false]
do...something
enabletimersub()
end sub
sub enabletimersub()
timer.enabled = true [or timer.start()]
end sub

thnx

Jared Parsons said:
Hello Ratnesh,
thanks jared,

but that not working either. i've tried both ways, even both together.
but no luck

What happens when you call Start( exception, or just nothing). Try
toggling the Enabled and not calling Stop() at all.
--
Jared Parsons [MSFT]
(e-mail address removed)
All opinions are my own. All content is provided "AS IS" with no
warranties, and confers no rights.
 
Hello Ratnesh,
when i call start or enabled=true. nothing happens in any case. but
the tick
event doesnt fire after the interval.
i've also tried to toggle the enabled property and didnt use Stop()
still no luck. i dont know whats wrong.
its like this
sub timer.tick()
timer.stop() [or timer.enabled=false]
do...something
enabletimersub()
end sub
sub enabletimersub()
timer.enabled = true [or timer.start()]
end sub

Can you post a code snippet that reproduces the issue?
 
its a huge code. but i think i found the reason. herez the deal

can this be a problem?
the do..something part runs on another thread which eventually calls the
enabletimersub()
so the main timer.tick() thread and enabletimersub() are on different
threads. is there any solution for this

sub timer.tick()
timer.stop() [or timer.enabled=false]
do...something
end sub
sub enabletimersub()
timer.enabled = true [or timer.start()]
end sub

sorry for the delay in telling this point


Jared Parsons said:
Hello Ratnesh,
when i call start or enabled=true. nothing happens in any case. but
the tick
event doesnt fire after the interval.
i've also tried to toggle the enabled property and didnt use Stop()
still no luck. i dont know whats wrong.
its like this
sub timer.tick()
timer.stop() [or timer.enabled=false]
do...something
enabletimersub()
end sub
sub enabletimersub()
timer.enabled = true [or timer.start()]
end sub

Can you post a code snippet that reproduces the issue?

--
Jared Parsons [MSFT]
(e-mail address removed)
All opinions are my own. All content is provided "AS IS" with no
warranties, and confers no rights.
 
here is the snippet

Private Sub enabletimersub()
MsgBox("hi")
tmr.Enabled = True
End Sub
Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
Handles tmr.Tick
tmr.Stop()
Dim thread1 As New System.Threading.Thread(AddressOf msgboxfun)
thread1.Start()
End Sub


Jared Parsons said:
Hello Ratnesh,
when i call start or enabled=true. nothing happens in any case. but
the tick
event doesnt fire after the interval.
i've also tried to toggle the enabled property and didnt use Stop()
still no luck. i dont know whats wrong.
its like this
sub timer.tick()
timer.stop() [or timer.enabled=false]
do...something
enabletimersub()
end sub
sub enabletimersub()
timer.enabled = true [or timer.start()]
end sub

Can you post a code snippet that reproduces the issue?

--
Jared Parsons [MSFT]
(e-mail address removed)
All opinions are my own. All content is provided "AS IS" with no
warranties, and confers no rights.
 
Hello Ratnesh,
its a huge code. but i think i found the reason. herez the deal

can this be a problem?
the do..something part runs on another thread which eventually calls
the
enabletimersub()
so the main timer.tick() thread and enabletimersub() are on different
threads. is there any solution for this

That definately could be causing the behavior you are seeing.

The solution to this is to use Control.Invoke() to set Enabled on the thread
the timer lives on.
 
thanks jared,
i found that too. but i m not sure how to use it. cna u show me in the
snippet or give me any weblink for the help??
Private Sub enabletimersub()
MsgBox("hi")
tmr.Enabled = True
End Sub
Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
Handles tmr.Tick
tmr.Stop()
Dim thread1 As New System.Threading.Thread(AddressOf enabletimersub)
thread1.Start()
End Sub


Jared Parsons said:
Hello Ratnesh,
its a huge code. but i think i found the reason. herez the deal

can this be a problem?
the do..something part runs on another thread which eventually calls
the
enabletimersub()
so the main timer.tick() thread and enabletimersub() are on different
threads. is there any solution for this

That definately could be causing the behavior you are seeing.
The solution to this is to use Control.Invoke() to set Enabled on the
thread the timer lives on.
--
Jared Parsons [MSFT]
(e-mail address removed)
All opinions are my own. All content is provided "AS IS" with no
warranties, and confers no rights.
 
Hello Ratnesh,
thanks jared,
i found that too. but i m not sure how to use it. cna u show me in the
snippet or give me any weblink for the help??
Private Sub enabletimersub()
MsgBox("hi")
tmr.Enabled = True
End Sub
Private Sub tmr_Tick(ByVal sender As Object, ByVal e As
System.EventArgs)
Handles tmr.Tick
tmr.Stop()
Dim thread1 As New System.Threading.Thread(AddressOf
enabletimersub)
thread1.Start()
End Sub

Assuming that Me is a Form or Control of some kind, try the following

Sub enableTimerSub()
Me.Invoke(CType(AddressOf enableTimerSubImpl, EventHandler))
End Sub

Sub enableTimerSubImpl(ByVal object As Sender, ByVal e As EventArgs)
tmr.Enabled = True
End Sub

Now you shoudl be able to call enableTimerSub() from the wrong thread and
it will invoke and call enableTimerSubImpl on the correct thread.
 
thanks again and again
it works (finally)
phew!!

Jared Parsons said:
Hello Ratnesh,
thanks jared,
i found that too. but i m not sure how to use it. cna u show me in the
snippet or give me any weblink for the help??
Private Sub enabletimersub()
MsgBox("hi")
tmr.Enabled = True
End Sub
Private Sub tmr_Tick(ByVal sender As Object, ByVal e As
System.EventArgs)
Handles tmr.Tick
tmr.Stop()
Dim thread1 As New System.Threading.Thread(AddressOf
enabletimersub)
thread1.Start()
End Sub

Assuming that Me is a Form or Control of some kind, try the following

Sub enableTimerSub()
Me.Invoke(CType(AddressOf enableTimerSubImpl, EventHandler))
End Sub

Sub enableTimerSubImpl(ByVal object As Sender, ByVal e As EventArgs)
tmr.Enabled = True
End Sub

Now you shoudl be able to call enableTimerSub() from the wrong thread and
it will invoke and call enableTimerSubImpl on the correct thread.

--
Jared Parsons [MSFT]
(e-mail address removed)
All opinions are my own. All content is provided "AS IS" with no
warranties, and confers no rights.
 
is it possible to do the same thing , If the timer control is in like class
module and not on any form.

Jared Parsons said:
Hello Ratnesh,
thanks jared,
i found that too. but i m not sure how to use it. cna u show me in the
snippet or give me any weblink for the help??
Private Sub enabletimersub()
MsgBox("hi")
tmr.Enabled = True
End Sub
Private Sub tmr_Tick(ByVal sender As Object, ByVal e As
System.EventArgs)
Handles tmr.Tick
tmr.Stop()
Dim thread1 As New System.Threading.Thread(AddressOf
enabletimersub)
thread1.Start()
End Sub

Assuming that Me is a Form or Control of some kind, try the following

Sub enableTimerSub()
Me.Invoke(CType(AddressOf enableTimerSubImpl, EventHandler))
End Sub

Sub enableTimerSubImpl(ByVal object As Sender, ByVal e As EventArgs)
tmr.Enabled = True
End Sub

Now you shoudl be able to call enableTimerSub() from the wrong thread and
it will invoke and call enableTimerSubImpl on the correct thread.

--
Jared Parsons [MSFT]
(e-mail address removed)
All opinions are my own. All content is provided "AS IS" with no
warranties, and confers no rights.
 
Hello Ratnesh,
is it possible to do the same thing , If the timer control is in like
class module and not on any form.

You'll need a reference to a control on the foreground thread. Then you
can use thatControl.Invoke() to get onto the correct thread and reset the
timer.
 
can you explain how?
bcoz the control doesnt seem to have the .invoke() method.

i m running timer in the Main module. which calls another class as a
saperate thread.

thnx

Jared Parsons said:
Hello Ratnesh,
is it possible to do the same thing , If the timer control is in like
class module and not on any form.

You'll need a reference to a control on the foreground thread. Then you
can use thatControl.Invoke() to get onto the correct thread and reset the
timer.
--
Jared Parsons [MSFT]
(e-mail address removed)
All opinions are my own. All content is provided "AS IS" with no
warranties, and confers no rights.
 
jared,

i have the normal windows.forms.timer control.

i've Main module with SubMain and this timer and few other things like
contextmenu, trayicon etc
now on timer.tick i m calling another class function on seperate thread. on
end of that thread i want to start timer again.

the problem is, if it was a form i could've called the invoke method simply.
but here i dont know how to implement this.
this is tray application, so it only stays in tray.

thnx

Jared Parsons said:
Hello Ratnesh,
can you explain how?
bcoz the control doesnt seem to have the .invoke() method.
i m running timer in the Main module. which calls another class as a
saperate thread.

Is control an instance of System.Windows.Forms.Conrol? If so it should
have an Invoke method.

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

--
Jared Parsons [MSFT]
(e-mail address removed)
All opinions are my own. All content is provided "AS IS" with no
warranties, and confers no rights.
 
Hello Ratnesh,
jared,

i have the normal windows.forms.timer control.

i've Main module with SubMain and this timer and few other things like
contextmenu, trayicon etc
now on timer.tick i m calling another class function on seperate
thread. on
end of that thread i want to start timer again.
the problem is, if it was a form i could've called the invoke method
simply.
but here i dont know how to implement this.
this is tray application, so it only stays in tray.

Add a shared field to your module of type System.Windows.Forms.Control.
On the main thread (the one where you originally created the timers), addd
the following lines

mySharedControl = New Control()
mySharedControl.CreateControl()

Since mySharedControl is a shared field you can access it from your background
method and use it to invoke back into the foreground thread.
 
Back
Top