Solved

Windows Service , fire event at a specific time

Posted on 2003-11-11
13
616 Views
Last Modified: 2008-02-01
Hello All-

 I have been reading about the System.Timers , and I have been able to build some apps that will fire at given intervals ( i.e. every hour, day , etc )

But how can I fire an event at a specific time? Would it be possible that the application could be firing at specified intervals, and then at a specific time, stop execution of the interval events and fire another event?


Any and all help is most appreciated..
Thanks


 Dim t As New System.Timers.Timer()


        Private Sub Form1_Load(ByVal sender As Object, _
               ByVal e As System.EventArgs) Handles MyBase.Load

            AddHandler t.Elapsed, AddressOf TimerFired
            t.Interval = New TimeSpan(0, 1, 0, 0, 0).TotalMilliseconds


            ' Would it be possible to fire another event at a specified time?
 
                IF  t.?????? = 3 AM  THEN
                       STOP FIRST PROCESS

                                   FIRE SECOND EVENT

                       START FIRST PROCESS AGAIN
                END IF





        End Sub


        Private Sub btnStart_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnStart.Click

            t.Enabled = True
            Label1.Text = "Process Started"
        End Sub

        Private Sub btnStop_Click(ByVal sender As System.Object, _
             ByVal e As System.EventArgs) Handles btnStop.Click

 
               t.Enabled = False
                Label1.Text = "Process Stopped"

         
        End Sub

        Public Sub TimerFired(ByVal sender As Object, _
             ByVal e As System.Timers.ElapsedEventArgs)

            Label1.Text = "Last Fired At  " & e.SignalTime.ToString
            ' Perform events here
            PerformMyStuff()


        End Sub

 
0
Comment
Question by:drakum
13 Comments
 
LVL 28

Expert Comment

by:iboutchkine
ID: 9724955
Why not to put your application on the scheduler?
0
 
LVL 4

Expert Comment

by:NetPointer
ID: 9725024
u can create a batch file which handles your application stop start etc..and put that batch file under windows task scheduler to run at a specific time.
0
 
LVL 10

Expert Comment

by:GoodJun
ID: 9725261
dim yourDate as Date
yourDate=cdate(text.encoding.ascii.getString("11/11/2003 3:00:00")) 'double check the format of date, I am not very confortable with it.

if yourDate.Subtract(Now).totalMilliseconds=0 then

end if
0
 
LVL 2

Expert Comment

by:UberDeveloper
ID: 9725627
i'm not sure about this but would the above code have to fire every millisecond to work?   I was gonna say to make it:

If yourDate = Now then
...
end if

but then that code would have to fire every second to work wouldn't it?

0
 
LVL 2

Expert Comment

by:UberDeveloper
ID: 9725809
maybe you can work off of this....This code works for me just set the 2:18 PM to anything you want

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

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Dim yourDate As String
        yourDate = Now.TimeOfDay.ToString
        If yourDate.CompareTo("2:18 PM") Then
            MsgBox("Good Work")
            Timer2.Enabled = False
        End If
    End Sub
0
 
LVL 2

Expert Comment

by:UberDeveloper
ID: 9725820
Crap...nevermind it doesn't work   :(


Might give you something to go on though.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:drakum
ID: 9726009
I appreciate all the help so far ( it has given me some things to look at )

My thinking is that I have installed countless apps/services (which Im sure we all have), and most of them have an update option that allows you to set the update at a specific time..

They are not using the TS to automate the updating.. So I assume that in the logic of the application they have a timer event which will fire when the System.NOW (or whatever it is) = Config Date/Time

Thanks again for all your help so far.. no wonder everyone likes this place so much!
0
 
LVL 2

Accepted Solution

by:
UberDeveloper earned 500 total points
ID: 9726011
ok this one does work....There HAS to be a better way to do this but I just opened a new project put a textbox and timer control on the form and then add the code

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

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Dim yourDate As String
        yourDate = Now.TimeOfDay.ToString.Substring(0, 8)
        TextBox1.Text = Now.TimeOfDay.ToString.Substring(0, 8)

        If yourDate.Equals("14:43:00") Then
            MsgBox("Good Work")
            Timer2.Enabled = False
        End If
    End Sub
0
 
LVL 2

Expert Comment

by:UberDeveloper
ID: 9726032
oh and for some reason "Now.TimeOfDay.ToString.SubString(0, 8) returns military time but I think that might be better cause then you won't have to worry about AM, PM

To test this just set the "14:43:00" to the current time plus one minute and then run the app...when the clock in the textbox clicks over to the time you set the msgbox will be displayed.
0
 
LVL 2

Expert Comment

by:UberDeveloper
ID: 9726665
Oh and one last thing...just take out the Timer2.Enabled = False so it will run everyday
0
 

Author Comment

by:drakum
ID: 9732051
Damn... you are UBER.. :)
 Thanks so much.. if there is a better way.. I havent found it .. Ive looked all over and this is the only place where I have been able to find an answer.

 Thanks alot to everyone who replied.. this was driving me crazy

 
0
 
LVL 2

Expert Comment

by:UberDeveloper
ID: 9732606
Thanks alot...one thing you might like to know.  The reason i have the .Substring(0,8) at the end of the yourDate line is because  

Now.TimeOfDay.ToString           returns   14:43:00.XXXXXXX

The x's are (I'm assuming) the seconds elapsed since midnight or something so I just cut it out.

0
 

Author Comment

by:drakum
ID: 9733492
Thanks for the followup explanation.. :)

 
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now