Could someone tell me how to include a timer inside of a class? I was trying to set up a panel class that has some generic information in it to be included in many of the form windows included in a bigger project. It would also have some other information of course. But the idea was to create an instance of this class in each form that I wanted the panel to be displayed in. I was hoping not to have to also add a timer control at design time to each form.

So, in the form, I would include this

Private pnl As New pnlGeneric(Me)

Everything else, including a timer.tick event to update the panel, is handled by the pnlGeneric class.

Here was my first attempt at including the timer :

Public Class pnlGeneric
    Private tdPanel As New StatusBarPanel
    Private tdBar As New StatusBar
    Private WithEvents tdTimer As Timer

    Sub New(ByVal cForm As Windows.Forms.Form)
        tdPanel.Text = DateTime.Now.ToString("M/d/yyyy hh:mm:ss tt")
        tdPanel.AutoSize = StatusBarPanelAutoSize.Contents
        tdBar.ShowPanels = True
        tdBar.Panels.Add(tdPanel)
        tdTimer.Interval = 500
        tdTimer.Enabled = True
        cForm.Controls.Add(tdBar)
    End Sub

    Sub update(ByVal sender As Object, ByVal e As System.EventArgs) Handles tdTimer.Tick
        tdPanel.Text = DateTime.Now.ToString("M/d/yyyy hh:mm:ss tt")
    End Sub
End Class

Recommended Answers

All 2 Replies

Can you tell me the bigger scheme of the design for your project? If you have each panel have a timer it will create a thread for every timer instance and if you have more than say 8 instances of the panel thus 8+ threads running you're going to murder your application's performance from context switching keeping all those threads in synch.

What you *could* do is have a single timer and have a private shared collection of all instances of the panel created .. and have a single timer poll the list of created panels and run the update code. This would use 1 timer for all panels.

Can you tell me the bigger scheme of the design for your project? If you have each panel have a timer it will create a thread for every timer instance and if you have more than say 8 instances of the panel thus 8+ threads running you're going to murder your application's performance from context switching keeping all those threads in synch.

What you *could* do is have a single timer and have a private shared collection of all instances of the panel created .. and have a single timer poll the list of created panels and run the update code. This would use 1 timer for all panels.

sknake, thanks for the response. At this point I'm not sure of the bigger scheme of the design. I was just trying to play around with some things and learn in the process before developing a more detailed design. Could you provide me with a simple example of your suggestion? Can this single timer be created through the class constructor?

Thanks in advance for your help with this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.