2

I have Console Application written in C# that is scheduled to run everyday so using the Built in Windows Task Scheduler. Every time it runs the black console box pops up for the duration of it's execution and then closes. I am not writing anything to the console.

1.I've found other questions with search results I did task scheduler. but I could not decide where I will write code in the ASP.Net Mvc web app. this is the code. I trying this one.

private void CreateTaskRunDaily()
{
    using (TaskService ts = new TaskService())
    {
        TaskDefinition td = ts.NewTask();
        td.RegistrationInfo.Description = "My first task scheduler";

        DailyTrigger daily = new DailyTrigger();
        daily.StartBoundary = Convert.ToDateTime(DateTime.Today.ToShortDateString() + " 16:30:00");
        daily.DaysInterval = 1;
        td.Triggers.Add(daily);               
        td.Actions.Add(new ExecAction(@"C:/sample.exe", null, null));
        ts.RootFolder.RegisterTaskDefinition("TaskName", td);
    }
}

I could not decide where should I put in practice.

2.I want to register only once, when the program runs.Thereby adding again each time it runs, over and over.I need to prevent it. If there is any advice it helps a lot.

Selman
  • 97
  • 2
  • 12
  • I'm not quite sure what you are trying to achieve here, are you trying to execute a console application on a schedule from an ASP.NET Application? – SimonGates Aug 27 '16 at 11:57
  • @SimonGates Yes and adding task scheduler but I do not know exactly where I need to add code? – Selman Aug 27 '16 at 12:03

2 Answers2

4

Running background tasks isn't trivial and you need to be careful, you can easily bring down your entire app domain.

This is a good post on the subject.

How to Run Background Tasks

I highly recommend Hangfire if you need to manage multiple jobs running different schedules. It's a great piece of open source software.

SimonGates
  • 5,961
  • 4
  • 40
  • 52
1

MVC isn't going to run scheduled task for you, not without hacks that don't belong in an MVC project. I'd recommend changing the project properties on your console application and set the output type to windows application. This will make it so the console does not show. You could also run as a different user.

If you're still keen, check this out: http://www.radicalgeek.co.uk/Post/10/running-a-task-on-a-schedule-from-an-mvc-web-application

Joe_DM
  • 985
  • 1
  • 5
  • 12
  • thanks for your sharing. I found this http://stackoverflow.com/questions/2686289/how-to-run-a-net-console-app-in-the-background I changed output type.it was very helpful. – Selman Aug 27 '16 at 12:12