Timer Issue

Hey,

Usually I am over moderating at ASP.Net, but today I have a question for you guys. I have a timer that just is not firing. Can anyone help Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Configuration
Imports System.Windows.Forms
Imports System.IO
Imports System.Data


Public Class Alert
    Inherits System.Windows.Forms.Form



    Private Timer1 As System.Timers.Timer


    Public Sub New()
        MyBase.New()

        InitializeComponent()

        Try
            Timer1.Interval = Convert.ToDouble(ConfigurationSettings.AppSettings("CheckInterval"))
        Catch

        End Try

        Me.Hide()

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Alert))
        Me.Timer1 = New System.Timers.Timer()
        AddHandler Timer1.Elapsed, AddressOf timer1_Elapsed
        Me.Timer1.BeginInit()
        '
        'Timer1
        '
        Me.Timer1.Enabled = True
        Me.Timer1.Interval = 4000
        Me.Timer1.SynchronizingObject = Me
        '
        'Alert
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(144, 54)
        Me.ControlBox = False
        Me.Enabled = False
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.Name = "Alert"
        Me.ShowInTaskbar = True
        Me.StartPosition = System.Windows.Forms.FormStartPosition.Manual
        Me.Text = "HelpDesk Alert"
        Me.WindowState = System.Windows.Forms.FormWindowState.Minimized

    End Sub

    Private Sub timer1_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
        ' CheckMessages()
        MsgBox("Test me")
    End Sub


End ClassI would appreciate any help that anyone can give me!

Thanks,
Jason Gaylord
jgaylord@aspalliance.com


Answer this question

Timer Issue

  • Christopher Gordon

    well, I'd say for a WindowsForms app, you'll want to use the System.Windows.Forms.Timer, not the System.Timers.Timer  Is there something you need specifically from the System.Timers.Timer
  • agthomas

    I'll try to use that timer. I was trying to use the System.Timers.Timer. That namespace is a bit different from the System.Windows.Forms.Timer. You don't have any sample code for the namespace I was trying to use, do you
  • Nikhil Acharya

    Erik,

    Thanks for the quick response. I tried what you said. Here is my code:Imports System
    Imports System.Collections
    Imports System.ComponentModel
    Imports System.Configuration
    Imports System.Windows.Forms
    Imports System.IO
    Imports System.Data


    Public Class Alert

        Inherits System.Windows.Forms.Form

        'Private Timer1 As System.Timers.Timer

        Public Sub New()
            MyBase.New()
            InitializeComponent()
            Try
                Timer1.Interval = Convert.ToDouble(ConfigurationSettings.AppSettings("CheckInterval"))
            Catch
            End Try
            Me.Hide()
        End Sub


        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub


        Private components As System.ComponentModel.IContainer
        Friend WithEvents timer1 As System.Timers.Timer

        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.components = New System.ComponentModel.Container()
            Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Alert))
            Me.Timer1 = New System.Timers.Timer()
            'AddHandler Timer1.Elapsed, AddressOf timer1_Elapsed
            Me.Timer1.BeginInit()
            '
            'Timer1
            '
            Me.Timer1.Enabled = True
            Me.Timer1.Interval = 4000
            Me.Timer1.SynchronizingObject = Me
            '
            'Alert
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(144, 54)
            Me.ControlBox = False
            Me.Enabled = False
            Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
            Me.Name = "Alert"
            Me.ShowInTaskbar = True
            Me.StartPosition = System.Windows.Forms.FormStartPosition.Manual
            Me.Text = "HelpDesk Alert"
            Me.WindowState = System.Windows.Forms.FormWindowState.Minimized
        End Sub


        Private Sub timer1_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles timer1.Elapsed
            MsgBox("Test me")
        End Sub

    End Class<b>It still doesn't work.</b> Have any other ideas  Again, thanks for the help!

  • todong

    Ok, sorry about that, I was just guessing.  I've never needed to use the Timer control yet...here's a sample that does work, at least I think it's what you're looking for!


    Public Class Form1
        Inherits System.Windows.Forms.Form

    #Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call

        End Sub

        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.components = New System.ComponentModel.Container()
    Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
    '
    'Timer1
    '
    Me.Timer1.Interval = 4000
    '
    'Form1
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(292, 266)
    Me.Name = "Form1"
    Me.Text = "Form1"

    End Sub

    #End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    MessageBox.Show("Boom!")
    End Sub

    End Class

  • markdrury

    I'm not sure how you're building your forms, but if you simply drag that component from the toolbox in VS.NET onto your form, they take care of all that. I created a new form, dragged the component on, set the Interval property, added the MessageBox.Show call to the Elapsed event, and it just worked. The call to BeginInit and EndInit have nothing to do with whether the timer works--comment those out, and it should work just fine. The BeginInit and EndInit methods are just there to indicate to the Timer that the initialization is beginning and ending, but when I comment those out (using your code), the form continues to work fine.

    The most important issue is the setting of the SynchronizingObject property--if you attempt to modify the form or its contents from the Elapsed event and you haven't set that to be some object that's in the same thread as the thread that created the form, you'll sooner or later run into trouble. Dragging the component from the toolbox onto the form takes care of this for you. 

  • ctclark1

    Nope, again, I've never used it!  ;) 

    Did you look at the VS.NET help   ms-help://MS.VSCC/MS.MSDNQTR.2003JAN.1033/cpref/html/frlrfsystemtimerstimerclasstopic.htm

  • ChrisEvans

    I ended up switching it to System.Windows.Forms.Timer. However, I did figure out the other way. I added Me.timer1.Container = CType((Me.timer1), System.ComponentModel.ISupportInitialize)).BeginInit() under the declaration of the new timer1.

    Thanks again for the help! :) 

  • DivideByNought

    Erik,

    Thanks for your help. I did look at the VS.Net help already. I still can't get it to work using that namespace. Don't know why. If you come up with anything, please let me know.

    Thanks again
    Jason

  • RithuSasi

    Hi Jason!  Does it work if you get rid of the AddHandler (not sure if you put that in or what) and use the Handles keyword on your sub
  • linearkieran

    In case you were wondering, this program compiles into Alert.exe. I have a config file called Alert.exe.config that contains the value called by line: ConfigurationSettings.AppSettings("CheckInterval")). The config file looks like this:< xml version="1.0" encoding="utf-8"  > 
    <configuration>
    <appSettings>
    <add key="CheckInterval" value="10000" />
    </appSettings>
    </configuration>

  • RichB2005

    Well, FWIW, the Windows.Forms.Timer (the Timer control) isn't pre-emptive, so you can't be guaranteed it will actually raise its Tick event anywhere near the time interval you've requested. The System.Timers.Timer class, which is what you get if you drag a Timer from the Components tab of the toolbox, IS pre-emptive, and is more flexible than the WinForms timer. The Timer control (the ol' WinForms timer) is useful for cases where you don't really care if the code runs at exact intervals, and it's a lot simpler to use than the System.Timers.Timer. There's a third timer, System.Threading.Timer, but that's not really meant for use on a Windows form.
  • Timer Issue