DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Mule 4 Continuous Integration Using Azure DevOps
  • Python Async/Sync: Understanding and Solving Blocking (Part 1)
  • Why Architecture Matters: Structuring Modern Web Apps
  • MongoDB Change Streams and Go

Trending

  • Feature Flag Debt: Performance Impact in Enterprise Applications
  • Run Gemma 4 on Your Laptop: A Hands-On Guide to Google's Latest Open Multimodal LLM
  • A Hands-On ABAP RESTful Programming Model Guide
  • How to Format Articles for DZone
  1. DZone
  2. Coding
  3. Languages
  4. Trigger C# Functions on Specific Intervals

Trigger C# Functions on Specific Intervals

In this post, we will learn how to trigger some C# backend code on a specific interval to build a simple solution to achieve this functionality.

By 
Jawad Hasan Shani user avatar
Jawad Hasan Shani
DZone Core CORE ·
Dec. 06, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
7.5K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

Sometimes we need to perform some action at 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 codebase that utilized .NET Events. I wrote an article on this topic, which you can read if you need some background information about the code we’ll use today.

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

Here is the application code for your reference:


Application code

Here we have very basic Printer and Reporter objects. Then we wired up an event handler which is raised whenever printer.Print() method is called.

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

Requirement 

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

Creating Custom SetInterval Method

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

.NET Tasks construct

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

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

Changing code in the Main method

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

Program output


Using Built-in Timer Object

Let’s see another example, which uses a timer class to achieve the same functionality. The following picture shows the code part which uses the timer class:

Timer class utilization

And here is the output of program execution:


Program execution output


Summary

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

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

Let me know if you have any questions or comments.

code style application Implementation Git POST (HTTP) .NET Event Object (computer science) Execution (computing)

Published at DZone with permission of Jawad Hasan Shani. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Mule 4 Continuous Integration Using Azure DevOps
  • Python Async/Sync: Understanding and Solving Blocking (Part 1)
  • Why Architecture Matters: Structuring Modern Web Apps
  • MongoDB Change Streams and Go

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook