M# Tutorials

Learn to build .NET applications with M#. Our step-by-step tutorials will get you up-to-speed rapidly.
If you already know ASP.NET and C#, you can master M# in a week or two.

Automated Tasks

The majority of Web Applications contain tasks running in the background in an infinite loop. The email service is a good example, the applications checks for new emails to send each minute and if there, they are sent and then wait another minute.

Creation

To create your first automated task, go to your project settings.

Expand the automated task section, click on a new line and type the name of your task, for example "Send email queue items", and press enter to create it.

As you can see M# generates a basic task template for you with a few prepopulated attributes.

Generated code

M# generated some C# code when you created an automated task. To understand what's happening under the hood, open your Visual Studio Solution and open /Website/App_Code/TaskManager.cs. This file is automatically generated and you can see your new task at the end of the file.

In this class there is a Run() method which runs all your automated tasks. This method is called in the Global.asax file.

Basic attributes

Let's try to send all emails each 30 seconds.

Code

This is the code that should be run by the automated task. It is a good practise to only make a call to a static method from here and not implement logic here. For example for using M# email service, the code will be EmailService.SendAll(); (For more information on EmailService class and sending emails please read tutorial Sending Emails).

Frequency unit

Pick in the list the frequency unit in which you want to run the task, in our example "Seconds".

Frequency value

Set the value for your frequency unit, in our example "30".

Other attributes

Comments

This is a comment for the automated task and has no impact on the generated code, just a note to the developer.

Custom frequency value

It has the same role as the Frequency attributes except that it is calculated. For example you may want to allow admin users to change the frequency in the application settings.

Execution criteria

Allows you to start the task only if the condition is met. For example we only want to send emails the first day of each month.

Is disabled

If you set this attribute to "True" your task will never be run.

Name

The name of your task.

Record failure

By default failures happening during the execution of the task are recorded in the database, in the ApplicationEvent table. You can disable this feature.

Record success

By default successes happening during the execution of the task are not recorded in the database. You can enable this feature and record in the ApplicationEvent table.

Startup delay

This allows you to add a delay before your task start. The expected value in a

TimeSpan

.

Sync group

This allows you to create a Sync group on your task, your task will be executed inside a C# lock.