Mohammad Dayyan
Mohammad Dayyan

Reputation: 22411

how to schedule a task in MVC4 C#?

I wanna create a notification system on my website?(something like stack-overflow)
How can we schedule a task for mailing the notification for users on each 24 hours?

Can we use MVC4 or we should use windows service ?

Edit:
My Experience with using FluentScheduler in 3 month within a MVC4 App .
FluentScheduler is easy to config and using but it doesn't run tasks any time. Sometimes run and sometimes doesn't run.
I think the best way for scheduling is Windows Service to ensure running a task at the specific time.

Upvotes: 21

Views: 20227

Answers (7)

Andreas Bergström
Andreas Bergström

Reputation: 14610

There is also a built-in option, QueueBackgroundWorkItem. It was added in .Net 4.5.2 and here is a guide on how to use it in MVC.

In addition to previously mentioned FluentScheduler you also have HangFire. And if you plan on deploying to Azure there's a handful of different services for this.

Upvotes: 0

Baqer Naqvi
Baqer Naqvi

Reputation: 6504

First of all;

  1. Add Nuget package Install-Package FluentScheduler
  2. Add your registry class that inherit Registry and some code
// Schedule a simple task to run at a specific time
Schedule(() => System.Diagnostics.Debug.Write("This is from service " + DateTime.Now.Second+"\n"))
               .ToRunNow().AndEvery(2).Seconds();
  1. Register that registry class in Application_Start of Global.asax.cs with TaskManager
TaskManager.Initialize(new Test());

GitHub

Upvotes: 1

Benoit
Benoit

Reputation: 1107

I use scheduled task of windows.

I have build a little app than enter a record in the bd, then access the website with this recordId(Guid) as a parameter.

In mvc i check if the id exist, if it exist i run tasks then delete the record in the db, if not i ignore it.

this way im able to add schedule with a param. without updating the app each time i need a new scheduled task. i just add a new task like "myapp.exe /MyNewTaskName"

hope this help someone ;-)

Upvotes: 1

user172163
user172163

Reputation:

You can use ATrigger scheduling service. A .Net library is also available to create scheduled tasks without overhead. Errors log, Analytics, Tasks Listings and more benefits.

Disclaimer: I was among the ATrigger team. It's a freeware and I have not any commercial purpose.

Upvotes: 3

heads5150
heads5150

Reputation: 7443

Found this to be an awesome scheduler, FluentScheduler

Usage:

// Schedule an ITask to run at an interval
    Schedule<MyTask>().ToRunNow().AndEvery(2).Seconds();

Upvotes: 16

phnkha
phnkha

Reputation: 7872

You need a .Net Job Scheduler. Here is a good one: http://quartznet.sourceforge.net/

Upvotes: 7

Jordi
Jordi

Reputation: 2797

maybe you wanna use a scheduled task. Doing this in a MVC is a bad idea (mixing responsabilities) and building a windows service looks like an overkill to me (because is something doesn't need to run all the time).

Upvotes: 2

Related Questions