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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vb.NET Timer not firing Elapsed time event 2

Status
Not open for further replies.

qjade

Programmer
Jun 7, 2004
42
US
Hello friends,
I have read numerous threads in this site as well as Google but still cannot seem to get a simple timer to fuction properly. All I want to do is create a timer that fires every 15 seconds and execute the code in my Elapsed Time Event. My simplified example is as follow:

'////////////////////////////////////////////
Imports System
Imports System.Timers


Public Class TimerTest

Public Shared Qtimer As New System.Timers.Timer

Public Shared Sub main()

Qtimer.Interval = 15000
Qtimer.Enabled = True

End Sub

Private Shared Sub Qtimer_Tick(ByVal sender As Object, ByVal e As ElapsedEventArgs) _
Handles Qtimer.Elapsed

MessageBox.Show("Event is FIRED!!!")
End Sub

End Class
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

For some reason, my code never reaches the sub Qtimer_Tick. Please shed some light into why this is not working for me at all. I am not using any form. This will be created as a service. I copied this sample code almost verbatim from one of the thread on this site.
Thanks ahead for any comments!!
 
hi
you need to declare Qtimer 'withevents' or add an event handler using the 'addHandler' method
hth
sin1965
 
Thanks for the advice sin1965. However, I have gone that route but still no luck. Here is my modifications:

'/////////////////////////////////
Imports System
Imports System.Timers


Public Class Qtest2

Public Shared WithEvents Qtimer As New System.Timers.Timer

Public Shared Sub main()
AddHandler Qtimer.Elapsed, AddressOf Qtimer_Tick
Qtimer.Interval = 15000 'Every 5 seconds
Qtimer.Enabled = True

End Sub

Private Shared Sub Qtimer_Tick(ByVal sender As Object, ByVal e As ElapsedEventArgs) _
Handles Qtimer.Elapsed

MessageBox.Show("In timer_click!!!")
End Sub

End Class
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
 
Yeah, my commenting is a little off from the numerous mods I have been trying. Please ignore that comment and proceed with your debugging help. Thanks.
 
Is sub Main your start up method?

If so, you need something to keep the application running. If nothing else happens in sub main, as soon as sub main completes, the application will end.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
you shouldn't have a sub main in a public class unless it is the start of an application. maybe you should change it to a constructor with the 'sub New'.
Also you do not need an AddHandler if you have declared withevents.
hth
sin1965
 
Rick,
Yes Main() is set as my start up method. I have also tried two separate methods to troubleshoot your idea of Main() ending too soon.
1) Wrap an infinite while loop around the code in Main()
2) Keep the timer object active with "GC.KeepAlive(Qtimer)".

However, they both again did nothing for me. I did run across a post via Google stating a possible issue relating to windows account setting. Have you or anyone ran across anything that resemble Window Accounts' privileges putting restriction on Timer being fired?

Thanks.
 
just for testing purposes, try this:
Code:
Public Class Qtest2

    Public Shared WithEvents Qtimer As New System.Timers.Timer

    Public Shared Sub main()
        Qtimer.Interval = 5000 'Every 5 seconds
        Qtimer.Enabled = True

        dim frmKeepAlive as new form
        frmKeepAlive.showmodal()
    End Sub

    Private Shared Sub Qtimer_Elapsed(ByVal sender As Object, ByVal e As ElapsedEventArgs) _
        Handles Qtimer.Elapsed

        MessageBox.Show("In timer_elapsed!!!")
    End Sub

End Class

The modal form should keep the sub Main from completing (until you close the form). That should give the Qtimer_tick event a change to fire.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I assume that you have some termination criteria, so as a slight modification to Rick's suggestion:

Code:
Imports System
Imports System.Timers


Public Class TimerTest

	Public Shared WithEvents Qtimer As New System.Timers.Timer

	Private Shared NumberOfTimes As Integer = 0

	Public Shared Sub main()

		Qtimer.Interval = 15000
		Qtimer.Enabled = True
		Dim f As New Form1
		Application.Run(f)

	End Sub

	Private Shared Sub Qtimer_Tick(ByVal sender As Object, ByVal e As ElapsedEventArgs) _
					Handles Qtimer.Elapsed

		MessageBox.Show("Event is FIRED!!!")
		NumberOfTimes += 1
		If NumberOfTimes > 2 Then Application.Exit()

	End Sub

and the form simply consists of (so that it doesn't show):

Code:
Public Class Form1

	Private Sub Form1_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated

		Me.Visible = False

	End Sub
End Class

Alternatively, (as you don't actually say what you are trying to achieve), you may want to look into creating a Windows Service.


Hope this helps.

[vampire][bat]
 
Thank you for all of your input. In trying to avoid the usage of Form I decided to go ahead and utilized System.Threading.Thread.Sleep(15000) instead. I am sure there are other more "programmitically-correct" method of getting what I need done but due to time constrain I went with that route. Thank you all.
 
This sounds like a great time for my favorite absurd variable. bCowsComingHome!

Code:
Imports System
Imports System.Timers


Public Class TimerTest

    Public Shared WithEvents tmrTest As New System.Timers.Timer

    Private Shared bCowsComingHome As boolean = false

    Public Shared Sub main()

        tmrTest.Interval = 15000
        tmrTest.Enabled = True

        While Not bCowsComingHome
            threading.thread.sleep(100)
        end while
    End Sub

    Private Shared Sub tmrTest_Tick(ByVal sender As Object, ByVal e As ElapsedEventArgs) _
                    Handles tmrTest.Elapsed

        MessageBox.Show("Event is FIRED!!!")

        if somthing = something else then
            bCowsComingHome = true
        end if
    End Sub
end class

The problem with Thread.Sleep is that the thread will do nothing while it is sleeping. The Timer will operate in a different thread, but anything on the primary thread will be subject to the sleep. Including any GUI objects that were created on the primary thread.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
... or, just because I prefer the more oblique syntax [wink]:

[tt]
bCowsComingHome = somevariable = somevalue
[/tt]

[vampire][bat]
 
I might be wrong but didnt you forget to call start() in sub main?

Code:
Public Shared Sub main()

        Qtimer.Interval = 15000 
        Qtimer.Enabled = True
        Qtimer.Start()

    End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top