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

Scheduled application launcher

Status
Not open for further replies.

kvang

Programmer
Oct 7, 2005
129
0
0
US
Help...I can't get my windows service to run this. I am trying to run a program at a schedule time. What am I doing wrong?


Public Class Service1
Inherits System.ServiceProcess.ServiceBase

' This fires every 60 seconds.
Private WithEvents ServiceTimer As New Timer(60000)
Private Counter As Integer
Dim CurrentTime As DateTime = DateTime.Now
Dim StartTime As DateTime

Protected Overrides Sub OnStart(ByVal args() As String)
ServiceTimer.Start()
End Sub

Protected Overrides Sub OnStop()
ServiceTimer.Stop()
End Sub

Private Sub DoWork(ByVal sender As Object, _
ByVal e As ElapsedEventArgs) Handles ServiceTimer.Elapsed
Try
StartTime = Convert.ToDateTime("2/28/2006 3:30 PM")
If (CurrentTime.Hour = StartTime.Hour) AndAlso (CurrentTime.Minute = StartTime.Minute) Then
Dim objStreamWriter As System.IO.StreamWriter
Dim strFilename As String = "C:\EventLogs.TXT"
If System.IO.File.Exists(strFilename) Then
objStreamWriter = System.IO.File.AppendText(strFilename)
Else
objStreamWriter = System.IO.File.CreateText(strFilename)
End If
objStreamWriter.WriteLine("The program was started at " & Now)
objStreamWriter.Close()
End If
Catch Err As Exception
Debug.WriteLine(Err.ToString())
End Try
End Sub
End Class
 
In a windows application project, the procedure is triggered when the timer ticks:

Code:
Private Sub ServiceTimer.Tick
   ServiceTimer.Enabled = False
   DoWork
   ServiceTimer.Enabled = True
End Sub

Is it different in a Windows Service application?

mansii
 
Once the windows service application starts, the timer is trigger with:

Protected Overrides Sub OnStart(ByVal args() As String)
ServiceTimer.Start()
End Sub

If I used:

(CurrentTime.Minute > StartTime.Minute)

instead of

(CurrentTime.Minute = StartTime.Minute)

the windows service will do what it's intended. The timer is on a 60 seconds interval. Suggestions how I can make this work?


 
Then try:
Code:
(CurrentTime.Minute [blue]>=[/blue] StartTime.Minute)

Regards,
mansii
 
Is there another way so that it will execute at the exact time only once? Since the interval is 60 seconds, this will trigger the application to execute more than once when:

Code:
(CurrentTime.Minute >= StartTime.Minute)
 
Any reason why you set:
Code:
StartTime = Convert.ToDateTime("2/28/2006 3:30 PM")
?

Would the CurrentTime will always be greater than StartTime?

 
I was setting that for testing only. What I really want to do in the end is have this program run once a day at a specified time (1:00 AM). Any suggestions?
 
I could only 2 ways:

1.
Code:
Dim StartTime As Date = DateAdd(DateInterval.Day, -1, Now)

Private Sub DoWork(ByVal sender As Object, _
      ByVal e As ElapsedEventArgs) Handles ServiceTimer.Elapsed

If DateDiff(DateInterval.Day, StartTime, Now) = 0 Then
 Exit Sub
Endif

StartTime = Now

'Do the job here

End Sub

2. Make a windows application, then put it in the Task Scheduler.

Hope this helps,
mansii
 
You could use my program. I call it EZ Run. One of it's features is to run any designated program at any designated time, for a designated amount of time. It then turns that program off. (Or can be set to start a program and not turn it off.) If interested, let me know.

Becca

Somtimes, the easy answer is the hardest to find. :)

Still under construction ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top