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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Overview of C# Async Programming
  • Mule 4 Continuous Integration Using Azure DevOps
  • [CSF] OfficeFloor: Going Beyond Dependency Injection
  • Application Memory Management in .NET Framework

Trending

  • Introduction to Retrieval Augmented Generation (RAG)
  • Event-Driven Architectures: Designing Scalable and Resilient Cloud Solutions
  • Non-Project Backlog Management for Software Engineering Teams
  • Role of Cloud Architecture in Conversational AI
  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
6.9K 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

  • Overview of C# Async Programming
  • Mule 4 Continuous Integration Using Azure DevOps
  • [CSF] OfficeFloor: Going Beyond Dependency Injection
  • Application Memory Management in .NET Framework

Partner Resources

×

Comments

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: