Way to execute a .Net application on specific days - c#

I have a requirement where an application which is run by windows service to be executed on specific days in a week(Sunday through Saturday). These days should be stored in a config file and can be changed by user at any time.
Can you please point to right direction in achieving this. Please let me know if you need any clarification on this.

I'd suggest using Windows Task Scheduler instead of a service to launch the application. It is designed for this scenario.

If You are in Windows environment, then you can use Windows Task Scheduler.
The following is for Windows XP, but the instructions are almost the same for other versions of Windows
http://support.microsoft.com/kb/308569

Simple. Windows Operating System TASK SCHEDULER. Start your application at specific days. Finished.

How about registering them to the scheduled tasks?
It already has a great interface and any user can deal with them, let alone your costumer's IT depeartment.
You can also create a tool that registers the scheduled task to windows with parameters according to the XML file.

I agree with all the above.
Windows Task Scheduler would be the best and easiest solution.
You could even write a console application and set it to run at given times.
Here is a small guide to console applications for scheduled tasks:
http://www.15seconds.com/issue/080508.htm

As everyone has answered, Windows Scheduler is already part of Windows and will do this for you. However, if you need a programmatic answer to this, you can use the following C# code:
// Read in your configuration file
// and I am assuming you are reading in the file and storing the
// Days of the week you need it to run in a string array
foreach (string DayToRun in MyStringArray)
{
if (DateTime.Now.DayOfWeek.ToString().ToUpper().Equals(DayToRun.ToUpper())
{
// Today is the day we need to execute.
// Do execution here
System.Diagnostics.Process.Start("C:\\MyProgramToExecute.EXE");
break;
}
}

Related

Run C# application as scheduled task

I have a C# application that was designed to run with a windows form but now needs to run as a scheduled task. I have had problems with this and I think it is because it needs to be "headless" in that it should have no concept of a user environment. The program has been written to run unattended in that it has an /AUTO arg which then will run from some defaults but the form is still shown which causes the problem.
I have looked around and I think there is a way to suppress the form in this situation but I can't find exactly how. Does anyone know how I can suppress the form and allow this application to run?
Take the logic required for the scheduled task out of your WinForms application and put it in a Console application. If you can reuse logic both places, move it into a shared library.
Convert your application to console mode and also check the "Hidden" checkbox in the 'General' tab of Task scheduler.
This will help you
Still if you want to use the WinForm application, then set its ShowOnTaskbar property to false.
And its very easy to convert your winform application to winform one. Just go to the project properties and change the output type to Console. But you need to do few tweaks in the code.
Check if the /AUTO parameter is set and then depending on whether or not is is. Change this in your Program.cs:
Application.Run(new Form1());
to
Application.Run();
this won't show a form and you can do whatever other things you like.
This is the best I can give you without having seen you code. Hop it helps!
You can tell the task to interact with desktop in which case a form that shows is not a problem. As long as your app will close by itself so the job finishes..
I made a form based thing and then wanted a scheduled task so wrote a commandline front end calling the form based app, and then pushed the 2 exes together with ilmerge so it cant get confused, because it was a cheap hack
If your code is properly written you can do an exe front end and use the same classes as your form (or dll) and work that way.
As far as I understood you, you do not need the Forms mode anymore, right?
If this is correct, I suggest implementing your application as service, e.g. a WCF service. It can permanently run and execute your business logic on a configured timer.
To make configuration of the timer easy and flexible, you could optionally imncorporate NCronTab. This allows you to schedule the task in a pattern as easy as 45 11 * * Friday (=> "Run every Friday at 11:45 am")

How to check time automatically?

I am writing a backup process.
Step1: User selects 3 times (Tuesday 1am,3am,10am or Everyday 1am,3am,10am)
Step 2: The application should check the time settings and start the backup process automatically.
Is it possible to start the backup process if the application is not running?
if your client/user is using Windows operating system then you can use Windows Task Scheduler to do the job. To schedule the job programmatically see this post and this post
why don't you use Windows Schedule ?
Sure, just use the Windows Task Scheduler. Windows will take care of running your application.
IMO you can use Windows Scheduler, that is the best. and Also do care of TimeZones :P :P
here are the links which may help you.
Task Scheduler

Creating VS 2010 executable for Daily Tasks

I want to create an executable in VS 2010. This executable will be create an excel spreadsheet and will transfer that file via FTP. I want this executable to be fired off via Windows tasks.
What is the best way to accomplish this? Would I create a regular windows form application, dll, or Empty Project, or windows service?
Thank you in advance for any assistance.
A plain old console application scheduled with the task scheduler should do the trick.
If you need the application to run when a computer is turned on but no one is logged in, create a service. If your application runs only when someone is logged in, but has no UI, use a console application. If your application runs only when someone is logged in and has a UI, use a Winforms app.
I'm not sure what the current best practice is, but in our shop we create console applications and use the task scheduler to execute them.
A library (dll) won't be executable from the task scheduler AFAIK, and a WinForms app isn't very useful for any app that runs automatically (i.e.: doesn't require user interaction).
A service would be appropriate for an application that needs to respond to system events/changes when they occur, which doesn't sound like your use case.

Automated task

I have a C# program that I want to run automatically every night without me having to do this,
I believe there is a way of doing this by setting an automated task however im not sure how to do this?
P.S. not sure if this helps but it has a .exe file in its library.
Thanks in advance.
Have a look at Windows Scheduled Tasks, not sure if it's available only on Windows Server or also XP, Vista, 7...
What about registering this as a scheduled task?
Go to Control Panel -> Scheduled tasks.
Here you can easily add your exe as a task and choose when and how often to run your task.
In Windows 7 this is changed somewhat, but you can find it in
Control Panel -> System and Security -> Administrative Tools -> Task Scheduler
You can schedule tasks to run at whatever interval you like. I don't know what version of Windows you are using but here's how to it in Windows XP
You can use "Scheduled Tasks" on windows.
You could also write your application as a "Windows Service". By this, it would run all the time on a machine, without someone creating a scheduled task.
Windows scheduled tasks is the right way.
Look here for more information.
And here for how to do it in Windows 7

Scheduled .NET code execution in hosted environment?

I've got about 40 lines of .NET code in a console application that read and RSS feed and store information in a database. I need this code to execute every night for as long as the RSS feed exists (indefinitely). Currently, I just launch the console app. from my home computer.
Because I can't trust myself to remember to do this every night, I somehow need to have this code hosted. I'd like to somehow have this app. or code (it could easily be put in an ASP.NET page codebehind and triggered to execute when the page loads) run automatically without me having to run the console app. manually.
Any ideas?
EDIT: I don't want to run this code from my computer; I can't guarantee my computer will not be hibernating or connected to the Internet every night.
EDIT: Right now I'm thinking spawn a background thread using BackgroundWorker in Application_Start of the global.asax, have it download the RSS feed once a day, and Thread.Sleep() the rest of the time.
Although controversial, Jeff Atwood blogged about how they accomplished this for SO using cache expiration. Check it out at https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/.
You could use a similar method and schedule the cache to expire at the time you want the process to kick off.
You could use the Windows built-in task scheduler to start the console application.
I would suggest just using the 'at' command instead of getting this small app hosted somewhere.
C:\>at /?
The AT command schedules commands and programs to run on a computer at
a specified time and date. The Schedule service must be running to use
the AT command.
Email your webhost. Some managed hosts will set up a scheduled task for you.
They will likely have different policies regarding how to call a scheduled task (i.e. some may require it to be an aspx page and not an EXE)
Have you looked at Quartz Scheduler for .NET?
Quoth the tagline: Quartz.NET is a full-featured, open source job scheduling system that can be used from smallest apps to large scale enterprise systems.

Categories