top of page
Writer's pictureThe Tech Platform

Trigger C# functions on specific intervals



Introduction

Sometimes we need to perform some action on specified intervals. For example I was working on an application where I needed to simulate some server actions to be triggered automatically. In this post, we will learn how can we build a simple solution to achieve this functionality.


Setting the Scene

I will be using an existing code base which utilized .NET Events.


However, you can use your own existing code for such purpose as well.


Here is the application code for your reference:

class Program
{
    static void Main(string[] args)
    {
        var printer = new Printer();
        var reporter = new Reporter();

        //Handle Wiring
        printer.printed += reporter.Reporter;

        //raise the event
        printer.Print();

        Console.ReadLine();
    }
}


The output of above code is shown as follows:


This starting code can be downloaded from this git repo (master branch).


Now, what we want is to call the printer.Print() method on specific interval e.g. after every two seconds.


Creating a custom SetInterval method

An easy way to implement such functionality is to utilize .NET Tasks construct as shown in the following method implementation:

public static async Task SetInterval(Action action, TimeSpan timeout )
{
    await Task.Delay(timeout). ConfigureAwait(false);
    action();
    await SetInterval(action, timeout);
}

This method takes an Action and TimeSpan as its arguments and then perform the action. After that it calls itself again.


Now, we can change our code in Main method with something like shown below:

static void Main(string[] args)
{
    var printer = new Printer();
    var reporter = new Reporter();

    //Handle wiring
    printer.printed += reporter.Report;

    //custom code example
    SetInterval(() => printer.Print(), TimeSpan.FromSeconds(2));

    Thread.Sleep(TimeSpan,FromMinutes(1));
    Console.ReadLine();
}


Here is the output which shows that printer.Print() method is being called after specified interval:



Using Built-in Timer Object

Let’s see another example, which uses timer class to achieve the same functionality.


The following picture shows the code part which uses timer class:

static void Main(string[] args)
{
    var Printer = new Printer();
    var reporter = new reporter();

    //Handle wiring
    printer.printed += reporter,Report;

    //custom code example
    //SetInterval(() => printer.Print(), TimeSpan.FromSeconds(2));

    //timer.example
    var timer1 = new Timer(_ => printer.Print2(), null, 0, 2000);

    Thread.Sleep(TimeSpan.FromMinutes(1));
    Console.ReadLine();
}


and here is the output of program execution:



Summary

In this post, we saw a very simple mechanism to execute some actions based on some interval. We saw two implementations for such purpose.


You can download the code from this git repo ( timerSample branch)



Source: Medium - Jawadhasan


The Tech Platform

0 comments

Recent Posts

See All

Comments


bottom of page