Home All Groups Group Topic Archive Search About
Author
5 Jul 2006 8:58 PM
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

Author
5 Jul 2006 9:00 PM
Jared Parsons [MSFT]
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]
jared***@online.microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
Author
5 Jul 2006 9:15 PM
Ratnesh Raval
thanks jared,

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

Show quoteHide quote
"Jared Parsons [MSFT]" <jared***@online.microsoft.com> wrote in message
news:61f143b4c7e8c86e527271e004@msnews.microsoft.com...
> 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]
> jared***@online.microsoft.com
> All opinions are my own. All content is provided "AS IS" with no
> warranties, and confers no rights.
>
>
Author
5 Jul 2006 9:37 PM
Jared Parsons [MSFT]
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]
jared***@online.microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
Author
5 Jul 2006 9:55 PM
Ratnesh Raval
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

Show quoteHide quote
"Jared Parsons [MSFT]" <jared***@online.microsoft.com> wrote in message
news:61f143b4c938c86e57b7995103@msnews.microsoft.com...
> 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]
> jared***@online.microsoft.com
> All opinions are my own. All content is provided "AS IS" with no
> warranties, and confers no rights.
>
>
Author
5 Jul 2006 10:06 PM
Jared Parsons [MSFT]
Hello Ratnesh,

Show quoteHide quote
> 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]
jared***@online.microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
Author
5 Jul 2006 10:24 PM
Ratnesh Raval
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


Show quoteHide quote
"Jared Parsons [MSFT]" <jared***@online.microsoft.com> wrote in message
news:61f143b4ce68c86e5bc88b419b@msnews.microsoft.com...
> 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]
> jared***@online.microsoft.com
> All opinions are my own. All content is provided "AS IS" with no
> warranties, and confers no rights.
>
>
Author
5 Jul 2006 11:21 PM
Jared Parsons [MSFT]
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]
jared***@online.microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
Author
5 Jul 2006 11:34 PM
Ratnesh Raval
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


Show quoteHide quote
"Jared Parsons [MSFT]" <jared***@online.microsoft.com> wrote in message
news:61f143b4d378c86e6638720550@msnews.microsoft.com...
> 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]
> jared***@online.microsoft.com
> All opinions are my own. All content is provided "AS IS" with no
> warranties, and confers no rights.
>
>
Author
5 Jul 2006 11:50 PM
Jared Parsons [MSFT]
Hello Ratnesh,

Show quoteHide quote
> 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]
jared***@online.microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
Author
5 Jul 2006 11:56 PM
Ratnesh Raval
thanks again and again
it works (finally)
phew!!

Show quoteHide quote
"Jared Parsons [MSFT]" <jared***@online.microsoft.com> wrote in message
news:61f143b4d468c86e6a45948593@msnews.microsoft.com...
> 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]
> jared***@online.microsoft.com
> All opinions are my own. All content is provided "AS IS" with no
> warranties, and confers no rights.
>
>
Author
6 Jul 2006 4:56 PM
Ratnesh Raval
is it possible to do the same thing , If the timer control is in like class
module and not on any form.

Show quoteHide quote
"Jared Parsons [MSFT]" <jared***@online.microsoft.com> wrote in message
news:61f143b4d468c86e6a45948593@msnews.microsoft.com...
> 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]
> jared***@online.microsoft.com
> All opinions are my own. All content is provided "AS IS" with no
> warranties, and confers no rights.
>
>
Author
6 Jul 2006 5:27 PM
Jared Parsons [MSFT]
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]
jared***@online.microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
Author
6 Jul 2006 7:15 PM
Ratnesh Raval
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

Show quoteHide quote
"Jared Parsons [MSFT]" <jared***@online.microsoft.com> wrote in message
news:61f143b51108c86efdf663197e@msnews.microsoft.com...
> 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]
> jared***@online.microsoft.com
> All opinions are my own. All content is provided "AS IS" with no
> warranties, and confers no rights.
>
>
Author
6 Jul 2006 9:08 PM
Jared Parsons [MSFT]
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]
jared***@online.microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
Author
6 Jul 2006 9:28 PM
Ratnesh Raval
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

Show quoteHide quote
"Jared Parsons [MSFT]" <jared***@online.microsoft.com> wrote in message
news:61f143b53298c86f1cc51ac857@msnews.microsoft.com...
> 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]
> jared***@online.microsoft.com
> All opinions are my own. All content is provided "AS IS" with no
> warranties, and confers no rights.
>
>
Author
6 Jul 2006 10:11 PM
Jared Parsons [MSFT]
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. 

--
Jared Parsons [MSFT]
jared***@online.microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
Author
5 Jul 2006 10:30 PM
Ratnesh Raval
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


Show quoteHide quote
"Jared Parsons [MSFT]" <jared***@online.microsoft.com> wrote in message
news:61f143b4ce68c86e5bc88b419b@msnews.microsoft.com...
> 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]
> jared***@online.microsoft.com
> All opinions are my own. All content is provided "AS IS" with no
> warranties, and confers no rights.
>
>