How to run a C# script periodically? - c#

Is it possible to run some C# script under Windows periodically? I mean something like a SQL job etc. I know the solution via SSIS but it's not exactly what I am looking for.

Basically, if your task is more than just periodical running of small piece of code, you are going to end up with windows service having BackgroundWorker thread, running some task on timer.
Here is some links to start with
Create windows server
How to use Background worker

I use task scheduler for that: http://windows.microsoft.com/en-sg/windows7/schedule-a-task
Press Windows + R, write "control schedtasks" and press enter to open the task scheduler.
Create a new task:
Set "Triggers" (run periodical) and "Actions" (run your script) in the menu and save.

Related

Console Application running in Task Scheduler without stop

I've created a C# console application that does some updates in a SQL server database. I've set that application in the Task Scheduler to run daily. The application is running fine, but I noticed that the task never stops but keeps showing "Running", I have to click "End" by myself to stop it.
How can I let the task stop by itself without forcing it to stop ?
The task would stop by it self under normal conditions.
What purpose does your application server? It's doing something to keep it alive, since a normal console application would just run its course and shut down.
You always have the options of just straight up murdering it at the end of it's job, but it would be a better choice to identify what is keeping it alive.
System.Environment.Exit(0);
Is it multithreaded? Do you have some backgroundworkers running or anything?
This has been solved by using the files created in Debug instead of Release in the scheduled task, but i'm not sure why is that.

Running .net exe file using task scheduler issue

I have a C# program that I need to run on a scheduled basis. The program will initially load the web browser control and do some task. I have tried running the program by itself and it works well. But it will not run when called by a task scheduler job. This is my only scheduled task that requires a GUI to run. Is there a workaround to do this?
For you to do this you have to set up the task scheduler to "Run Only When User is logged on"
See picture:
[]1[]
More details on my blog on how to do this.

Calling a console application on timely basis using windows scheduled task . Is this a valid approach

This is about ASP.NET MVC background. When I need to call action methods on timely basis, I use such 3rd part tools as HangFire, which runs under the application pool. I want to create a sync job, which reads a .csv file generated by our ERP system and update our custom DB.
But I know that having this sync job as an action method inside of my ASP.NET MVC is not the correct way of doing things, as background jobs should not be part of my web application.
So to handle my sync job I did the following :
Using Visual studio 2012 , I created a new console application and inside its main method I wrote the sync job as follow:-
class Program
{
static void Main(string[] args)
{
using (Entities sd = new Entities())
{
//code goes here
sd.SaveChanges();
}
}
}
How can I call this console application on timely basis? I am thinking on creating a new task using windows task scheduler, which would be executed each hour and call the application, mainly calls the .application file?
now my question is how i can call this console application on timely basis? . i am thinking on creating a new task using windows task scheduler , which will be executed each hour and call the application, mainly calls the .application file?
Running a program is one of the things the Windows Task Scheduler (TS) lives for. So yes, please do use the TS.
Additional benefits of using the TS are:
Able to specify the user account to run the process under
Multitude of ways to specify a trigger for when the task should run
Conditional mechanisms to control if the task should run (computer idle; power savings)
And a jolly nice history of the task when run
The alternative is creating you own scheduling service just to run your app but such a choice is frowned upon due to:
reinventing of the wheel
Windows gets flooded with an inordinate number of proprietary task scheduling services all essentially doing the same thing.
Background Jobs in Web Apps
Though one could arguably spawn/schedule a background thread/worker item in say ASP.NET to invoke some c# code, the danger here is that IIS is unaware that you have done so. It may decide to suspend or recycle your App Pool due to web inactivity and then your scheduled jobs would be sent to the void. Hence why it is not recommended.
Now you could always tell IIS not to timeout your App Pool so that it is not recycled periodically but I suspect that will lead to other issues.
Best not to do so in the first place.
However, there is nothing wrong in ASP.NET using the Task Scheduler API to schedule an operation to run say a console app.
Windows Task scheduler is a perfectly valid way to call a .net console application. I have several that I wrote and support that have been running for years with no issues.

Run ASP.net c# Web Application Automatically

I have a ASP.net web application that checks the status of my servers, it then wraps all this information up and puts it in a email. My Question how do I run this automatically say every day at like 2:00am, or like every 12 Hours?
Thanks
The best solution is to create a simple MS Windows Service which will do this job.
You'd better implement this as a separate process from your ASP.NET application. Phil Haack has summarized the reasons in this blog post. A Windows service for example or even a console application using the windows scheduler could work just fine for this task.
You want a scheduler - I recommend Quartz.NET.
As others have said, your code doesn't have to be in a web app.
If it is, then schedule a job that uses WebClient to make a request to your web app.
Check out WebDriver.
It's intended as a test / qa framework, but there's nothing stopping you from using it in a console application, which you can then run as a Scheduled Task. Note that whatever machine runs the Scheduled Task has to have a browser that WebDriver can fire up.
The easiest solution would be to create a scheduled task on your server using the Windows Task Scheduler and setup this job so that it uses internet explorer to visit your webpage.
If you open the task scheduler and create a new task. In the "Run" field put:
"C:\Program Files\Internet Explorer\IEXPLORE.EXE""http://www.yoursite.com/yourpage.aspx"
Then in the "Start in" field put:
"C:\Program Files\Internet Explorer"
Now configure this task to run at 2:00am every day.

Task scheduler stops running C# console application periodically

I have a task scheduler which runs a C# console application every minute. It runs the .application file via a .bat file and does so successfully for a period of time before stopping completely.
Un-installing/re-installing my console application doesn't fix the problem and the task scheduler is showing the batch file as succesfully executing. Also, running the program manually works just fine.
My questions are:
How can I get this task to run again via the task scheduler. I have tried deleting and re-creating the task, uninstalling/reinstalling the applcation.
I have a scheduled backup task occuring around the time the application stops working. Volume shadow copy is not enabled. Could this be impacting my application and why?
In the task scheduler take a look at the Settings tab. You will find an option "If the task fails, restart every".
By default (this is not checked) then if your task fails it will not be run again. You can check your task history to see if it has failed. In any case it sounds like you want to be run again even if it failed the last time it ran.
This is the option you want to change.
Despite the help, I did not manage to isolate the cause of this problem.
I have re-developed my scheduler using Quartz.NET and it's now running as a Windows service.

Categories

Resources