Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timer Issue

Status
Not open for further replies.

dbsquared

Programmer
Nov 7, 2002
175
US
I am having a problem with setting up a timer seems stupid I know.
I have the timer in the form called timer1 (real intuitive I know)
Anyway here is what I have been trying to do. I want the timer to refresh my form say every 5 seconds or so. so I have coded in the timer event:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.Refresh()
Timer1.Start()
Application.DoEvents()
MessageBox.Show(" I am refreshed")
End Sub

When I click a button I have:

Private Sub cmdReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReport.Click

Timer1.Enabled = True
ReportGenerator()

End Sub

If I leave out the call to the other sub the timer works however when I call the other sub (which calls other subs)I get no timer any more, I know this because the messagebox doesn't pop up anymore.

Probably very simple and I am missing it. I tried the help in .net and it gave me no clue what I am missing, I have done a search in the forum but I didn't find any basics of the timer event that would help me here.

Thanks for your time.

To go where no programmer has gone before.
 
for one you should leave out the timer1.start in the timer1.tick handler you should do this in the form load. I have no idea why you want to enable the timer. If it is started it should be enabled. If you want to stop the timer then do timer1.stop. The biggest problem. is the timer, it is part of the process and when the processor is busy it won't tick. If you want a better timer use the one in the system.threading namespace. It is a little bit more difficult to use but it will always work on the specified time. BTW be sure the process you started is finished before your threading.timer ticks again.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
I thought that I would have to restart the timer every time if I want it to so that it would go off every 5 seconds. Is this not true?
However I don't want to start the timer to refresh the form until I kick off the rest of my code.

To go where no programmer has gone before.
 
the tick will happen every 5 seconds, or the time you set it to from the moment you start it. And you can stop it whenever you want and restart it when you want.

Look at the marquee control to see an example of how the threading timer works.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
I have .net 2002 so I have to convert it to that so that I can see what you have there so I will do that in a little bit.
Thanks fo your time I tested what you said and you are right about the timer.

To go where no programmer has gone before.
 
for your marquee contro I get a wierd warning on the fom1.vb when compiled.
Object type cannot be converted to target type.
and it brings me to the Public Class Form1
when i try to debug it I am using .net 2k2



To go where no programmer has gone before.
 
I don't have 2k2.

but oyu could look at the timer function int the usercontrol and how it is done.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
I am confused I see

Private Sub Marquee_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.MouseEnter
Timer2.Stop()
End Sub

Private Sub Marquee_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.MouseLeave
Timer2.Start()
End Sub

Private Sub Marquee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
objGraphics = Me.CreateGraphics
CalculateHeightsAndWidthsAndRectangles()
objtimerDelegate = New System.Threading.TimerCallback(AddressOf ScrollThread)
objTimer1 = New System.Threading.Timer(objtimerDelegate, objTimer2, 0, intTimerSetting)
objTimer2 = objTimer1
End Sub

However I don't see what the objTimer1 and objTimer1 are doing or why you are starting and stoping timer2 because I see no events for any of them.

Please explain what objTimer1 and objTimer2 are doing.
Thanks

To go where no programmer has gone before.
 
ah yes you found on of the errors I corrected today

I solved it by adding a bolscroll and did a check in scrollthread to see if it was true(scroll) or false(stop scroll).
sorry about that but the threading timer is a bit more dificult to stop then the other timers.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
can you post that code to either planetsource or here so that I can see what you mean.
Thanks

To go where no programmer has gone before.
 
I will tommorow that is in 12hours

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
I got this to work and was simple here is the code:

Code:
Public Class Timertest
    Public Tmr As System.Threading.Timer()
End Class

Public Class frmMain

Public Sub cmdReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReport.Click
        Dim mytmr As New timerTest()

        Dim timerDelegate As New System.Threading.TimerCallback(AddressOf CheckStatus)

        Dim timer As New System.Threading.Timer(timerDelegate, mytmr, 10000, 10000) ' waits 10 sec then invokes every 10 seconds.

        ReportGenerator()

    
        timer.Dispose()
        timer = Nothing
    End Sub

    Shared Sub CheckStatus(ByVal State As Object)
        Dim s As timerTest = CType(State, timerTest)
        Dim myform As Form
        myform = frmMain.ActiveForm
        myform.Refresh()
        MessageBox.Show("I am refreshed")
        'myform.Dispose()
        myform = Nothing
    End Sub

End Class

Worked like a charm.
by the way the Public Class Timertest must be in a seperate class module or will crash the form load at least in .net 2k2.

To go where no programmer has gone before.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top