(C# Language) Practice Exercise in C# - delegates

Create a class MyClock that contains only one public function "void Tick(Object?)". This signature matches the signature of the TimerCallback delegate defined in the System.Threading.Timer class of dotnet. The "void Tick" function prints the current system time on the console (use DateTime.Now). Inside Main create a System.Threading.Timer by using a suitable constructor that calls "void Tick" once every second. Run the program to verify that you have a clock that prints the current system time. You will have to study the documentation of System.Threading.Timer class.
(Rev. 19-Mar-2024)

Categories | About |     |  

Parveen,

Objectives of this Exercise

This exercise introduces you to

  1. the use of delegates
  2. the TimerCallback delegate of the built-in Timer class
  3. the importance of the Dispose method for IDisposable implementations

Video Explanation (see it happen!)

Please watch the following youtube video:

The Solution

Create a C# .NET Core console project as usual. Add a class called MyClock. You can obtain the completed project from the downloads attached to this video. Now I will discuss the class.

We have to add a function of a signature that matches the TimerCallback delegate defined in the built-in Timer class. The msdn documentation says public delegate void TimerCallback(object? state);.


// MyClock.cs file 

// source code is attached as 
// a download to this tutorial 

internal class MyClock
{
  // matches the TimerCallback delegate 
  public void Tick(Object? state)
  {
    Console.Clear();

    Console.WriteLine(DateTime.Now);

  }

}
    
  

We have added public void Tick function to this class.

The function clears the console first. This ensures that the display is of one single line each time. It gives better looks.

Current time is obtained by a call to DateTime.Now. You can format it in whatever way you like.

Program.cs File

Open the solution explorer again and open the program.cs file. Let us study the code line by line.

First we have created an object of the MyClock class.


// program.cs file 

using ConsoleApp1;
using System.Threading;

MyClock clock = new MyClock();

Timer _timer = new(
  clock.Tick,             /* TimerCallback */
  null,                   /* custom data - null */
  TimeSpan.Zero,          /* starting delay */
  TimeSpan.FromSeconds(1) /* callback periodicity */
  );

Console.WriteLine("Press Enter key to quit");

// hold the program so that it doesn't quit 
Console.ReadLine();

// a timer is provided by the OS 
// so we need to call the dispose 
// method to release un-managed resources 
_timer.Dispose();

Next we have the Timer object. The first argument is the TimerCallback. The second is a custom data that can be passed to the callback. We have kept it null because we do not need it. The third argument is the delay after which the timer starts. We want it started immediately. The last argument is the Timespan after which the timercallback is repeatedly called by the timer object.

Finally we have used the console readline call to prevent the Main function from exiting immediately.

The last call is a call to the Dispose method. This call is required because the documentation states that a Timer implements an interface called IDisposable. A class that implements this interface uses resources through the API of the operating system. Experts suggest that these resources should not be held for a long time. The Dispose function should be called as early as possible to release those resources. It is not very important in our case because the program is exitting anyway, and the garbage collector is about to release them now. But we have done it because good actions should be done as a habit.

Run the Program

Now we can run the program to verify that the clock indeed shows the correct time.

Thanks!


This Blog Post/Article "(C# Language) Practice Exercise in C# - delegates" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.