Tuesday, March 27, 2012

Automating Daily Database Inserts...Contd

I used thetutorial on creating a windows service. i was able to succesfully create a service. however, after going through the whole tutorial i realized this is something that is always running. i need to run the service only once a day ( usually past midnight).

does anyone know how i can configure it so it runs only at a specified time and not always.

thanks.The service will always be running since that's how services work. The service periodically checks every Timer.Internval milliseconds whether something should be run. When it actually runs something, like a report at midnight, is up to your code.

If you are concerned about the resources used by the service you can open up the Windows Task Manager and look under the Processes tab. My own similar scheduler uses a negligible amount of CPU. It barely ticks over a few seconds per 24 hours.|||so how do i set it to run at a certain time..all my calculations r dependent on the date functions...so i need the service to run after 12 midnight. so how do i compare the timer to the time of the day...
do you know of any tutorial or some sample...

thanks McMurdoStation|||The service is always running (that is the beauty of it). You need to code it such that it looks at the time of day and runs your process when required. In your case, you can have it check the date periodically, and then whenever the date changes, run the process.|||


// C# but this should give you the general idea

// Declare class level variable to hold date process last run
private DateTime lastRunDate = System.DateTime.Today;

// then handle the event the timer generates when each Timer.Interval has elapsed
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
DateTime today= System.DateTime.Today;
if( today > lastRunDate){
lastRunDate= today ;
RunYourStuff();
}
}

|||thanks both of you. i understand it better now.
however, i dont know what i messed up. i have been fiddling with it for some time now. i had already created 2-3 windows applications- one for creating a bunch of html pages , another for opening each of these files in a word app and printing it. now i was trying to merge all these processes into one windoes service. so i cut/pasted some code..etc. now it throws an error :
"
cannot start service from command line or a debugger. A Windows Service must first be installed (using instalutil.exe) and then started with the Server Explorer, Windows Services Administrative tool or the NET START command."

i am pretty sure its not the code. also when i went to administrative tols -> services -> and browsed for my "Service1"... it shows up in the list but its not started. i was trying to start it. it says "

"The service1 on local computer started ans then stopped. some services stop automatically if they have no work to do, for example, the Performance Logs and Alerts Service."

can you help me figure this out...

thanks in advance|||anyone..|||Did you create the installer for the service? I think that article talks about that too. After you create and run the installer then you can start the service from the Services Manager (if it didn't start automatically).

If the service is crashing still then add some code to write out the event log so that you can figure out where it is when it crashes.|||actually i was able to start the service...i can see it from the admin tools -> services

i tried to schedule it using windows scheduler and when the program ran at the specified time
.. it kept throwing the error :

"cannot start service from command line or a debugger. A Windows Service must first be installed (using instalutil.exe) and then started with the Server Explorer, Windows Services Administrative tool or the NET START command."

i dont understand why we have to create a set up project..
when i schedule the task.. i should select the windowsservice1.exe right ?
i googled around for some time, but most of the articles are too brief..

thanks|||You do not have to schedule the service to run. It is designed to CONSTANTLY run, with or without a user logged in. You need to check to make sure the service continues running (if it stops running, there is a problem in your code) and if it is running, internally in the code, you need to make it do what you want to do periodically (whatever period you need).|||when i tried to build the solution i get this error:

WARNING: This setup does not contain the .NET Framework which must be installed on the target machine by running dotnetfx.exe before this setup will install. You can find dotnetfx.exe on the Visual Studio .NET 'Windows Components Update' media. Dotnetfx.exe can be redistributed with your setup.

i really need some help in getting this running.

doug, like you said i removed it from the scheduled events. i coded it as :


Dim lastrun = System.DateTime.Now.Hour
Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
Dim today As DateTime = System.DateTime.Today
If today.Hour > lastrun Then
lastrun = today
'add monthly charges
Call addmonthlycharges()
'create the html statements
Call createstmts()
'print the stmts
Call printstmts()
End If
End Sub

so it will run every hour ( for now ) though it needs to run once a day.
i was trying to debug the program but it keeps throwing back the error :
"cannot start service from command line or a debugger. A Windows Service must first be installed (using instalutil.exe) and then started with the Server Explorer, Windows Services Administrative tool or the NET START command."

thanks|||someone...?|||anyone......|||The warning message about dotnetfx isn't a big deal. Presumably you have the DotNet framework already on your computer so it won't matter. It would only become an issue if you want to deploy your service on a server that doesn't already have DotNet. You can worry about that later...

To install the service follow the directions on that articlehttp://authors.aspalliance.com/hrmalik/articles/2003/200302/20030203.aspx">starting on this page.

You've already done this given the dotnetfx warning message. After it has built the install file (something.msi) right click on the installer project in the solutions explorer and select "Install" from the pop-up list. Either that or navigate to the something.msi file it created an double-click.

Follow the usual instructions for the install wizard.

After the install is done go to the services manager, look up the service you just installed, and start it. In principal, it should then start working and running your update at midnight.|||i can see the status of the service as "started" under services, but its not doing anything.. the prog is actually supposed to create a new folder and a few html files inside the folder and also print them.

heres the entire code :


Public Sub New()
MyBase.New()

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

' Add any initialization after the InitializeComponent() call

If Not EventLog.SourceExists("MySource") Then
EventLog.CreateEventSource("MySource", "MyNewLog")
End If
EventLog1.Source = "MySource"
EventLog1.Log = "MyNewLog"

End Sub

Dim FileExists As Boolean
Dim lblmessage As String = Now()
Dim lastrun = System.DateTime.Now.Hour

Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
EventLog1.WriteEntry("Starting")
Timer1.Start()
End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
EventLog1.WriteEntry("Stopping")
Timer1.Stop()
End Sub

Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
Dim today As DateTime = System.DateTime.Today
If today.Hour > lastrun Then
lastrun = today
'add monthly charges
Call addmonthlycharges()
'create the html statements
Call createstmts()
'print the stmts
Call printstmts()
End If
End Sub

i am sure theres no prob with the code, since i had the same code in a windows application and it runs fine. i was just trying to automate it so it runs by itself everyday...

i'd really appreciate any help in this..
thanks

No comments:

Post a Comment