I need to implement a windows service that performs database import, and that once a month.
The program receives data via e-mail, and will import them at the end of each month.
Is there a better way than set the program to sleep for max_integer seconds/miliseconds repeatedly ?
I would not do it as windows service. I would run it as a scheduled task.
Your service will just be sleeping for a month and is just a a waste of resources. Let the OS keep track of the time and start your application to do the once a month processing.
If you can avoid writing a window service, you'll make your life easier - scheduled tasks may be a better option here. Windows services are best used for things where there is some constant background activity that needs to happen - not for running tasks over long timescales.
However, if you must make this a windows service, then you don't want to set a long-timer sleep timeout. That's most definitely a problematic approach. What happens if your application is restarted? How will it know how long it's been sleeping for? Or what if the thread is restarted?
A better approach is to write a record somewhere in the database that identifies when the next import should happen as a date/time. You windows service can wake up periodically (every few minutes or hours) and see if the current date/time is greater than that value. If it is, run the import and update the next run date in the database to the next time to run.
If your app is restarted, you simply read the value back from the database and continue as before.
Related
I have a Windows service that is calling a stored proc over and over (in an infinite loop).
The code looks like this:
while(1)
{
callStoredProc();
doSomethingWithResults();
}
However, how there might be cases where the loop gets stuck with no response, but the service is still technically running.
I imagine there are tools to monitor the health of a service, to let operations teams know to restart it.
But for my scenario this won't help since the service will still be technically running, but it's stuck and can't continue.
What's the best way to ensure this process restarts if this scenario happens?
Would the solution be to use a task scheduler that checks for the heartbeat of this process, and restarts the service if it there's no heartbeat for a period of time? To have another separate thread that monitors the progress of the first process?
Windows services have various recovery options which takes care of question 1. For question 2, the best bet would be to use a timeout approach whereby if the service takes more than X amount of time to complete it restarts or stops what it's doing (I don't know the nature of your service so can't provide implementation detail).
The heartbeat idea would work as well, however, that just becomes another thing to manage/maintain & install.
I'm developing a Windows Service that will run every 15 minutes, and sends out push notifications to iOS, Android, and BlackBerry users. Each of these device specific operations will run in separate threads in the Windows service. That's all well and good, but there is a chance that we will need to send out up to 50,000 push notifications at a time. If this happens, it could possibly take more than 15 minutes, so before it's time for the next service to run, I want to know if the previous process has finished, and if it hasn't, wait and queue the Windows service to execute once the prior execution is complete. I'm fine with the threading aspect, but I don't know the correct way to implement the scenario that I've described above. Is there some sort of "Wait" or "Queue" mechanism in C#?
Since your service is using a Timer to schedule the work, it can always disable the timer when work begins, and reschedule, as needed, at the end of your work.
This allows a lot of flexibility. If a queue of work took more than 15 minutes, you could decide whether to delay the next one, just start it immediately (potentially running "forever"), skip it entirely, or whatever you needed, as the timer won't run again until it was reenabled.
I have a console application which should periodically listen remote database,
if there is a new value then do some stuff.
Normally I create windows task scheduler job to run this console app every 2 minutes.
Another option I think, in console app I will have a code like;
while(true)
{
ConnectDatabaseAndProcess();
System.Threading.Thread.Sleep(120000);
}
So I assume console app will be always open, and will wait 2 minutes for every process and continue.
In performance matter will it make any difference?
Unless your computer is so overloaded that the time to create a process every two minutes is a huge drain on resources, there's no benefit to having your program sitting in a loop waiting for two minutes, just so that it can poll the database.
The benefit of using scheduled tasks is that you can change the scheduled task frequency (make it once every five minutes, or once an hour, or whatever) without having to modify the program. Sure, you could use an application configuration file, but why? Why duplicate functionality that already exists in the operating system, and is more flexible.
Also, with a scheduled task, you know that the program will start the polling operation again the next time the computer is rebooted. If you depend on the program to provide that delay, you have to either remember to start it every time, or put it in the startup task list.
Also, when the program is sitting there idle, it's occupying memory that could be used by other processes.
All told, using scheduled tasks is a much more flexible and robust solution. Any marginal performance gain (and we're talking, at most, one second) from having the program always running is far outweighed by the disadvantages.
I would like to write an application in C# which runs in the background most of the time. It should only show a TrayIcon. For this I found a tutorial: http://www.simple-talk.com/dotnet/.net-framework/creating-tray-applications-in-.net-a-practical-guide/
But how can I tell my program to run every hour? Whats the best way to implement this. A timer? The app should use as less as possible resources while doing nothing.
thanks
Don't have a program that runs all the time but only performs activity every hour. Write a program that performs the activity and then schedule it hourly using Task Scheduler in windows.
Question: is the program you're doing a "user space" program, or should it run even if a user is not logged in? In other words, should it always be on?
Basically, are you doing something useful to a user, or is this for a business task like archiving a web server's log files to database?
If it's the former, keep doing your notification area program. If it's the latter, skip the notification area program and build a full-out Windows Service.
In both cases, use a timer; resource use will be minimal.
Not sure of the processing cost, but you could code in a sleep timer to put it to sleep for an hour in a loop so it will wake up, run, then sleep again. Not sure of the drain on resources when sleeping though. Also, if the program might take a few minutes, you could calculate sleep time based on the DateTime.Now DateTime object so it wakes up every hour on the hour.
I have this question from a client, to run some database scripts on a daily or biweekly basis, depending on the type of period. They have defined a 'busy' period and a 'quiet' period. They are using shared hosting for their applications and they can't add SQL Server jobs to their database.
What I want to do is create a windows service, which is configurable using a config file, containing the different periods and their intervals.
This service is checking the current time and date versus the scheduled periods in the configuration file and will execute a sql server stored procedure once the interval matches
My question: Is there a better way to do this kind of scheduling without sql server or do you know if an existing, more generic (free) solution already exists?
[Edit]
I can't use windows task scheduler since their quiet period is during winter and their busy period is during summer. With task scheduler I'd have to modify the scheduled tasks manually twice a year. That's now what I nor my client wants.
[/Edit]
Thanks
You could use "Scheduled Tasks" feature of windows to start some console application whenever it is required.
If you want to create your own scheduling solution, then I'd recommend to have a look at Quartz.NET, which offers the same flexibility as the windows task scheduler.
Why not use the task scheduler and then every time your application gets started, it checks it's own schedule to work out whether or not it needs to do something.
So you could use the task scheduler to start your application every 15 minutes. When your application starts up it uses it's own configuration file to work out whether or not it needs to do something.
You could also use this to get your application to stop any currently running versions of your application to kill any long running tasks!