DEV Community

Abhilash Panicker
Abhilash Panicker

Posted on

Building a Simple Pomodoro Timer in C# on Ubuntu/Windows with .NET Core

Introduction:

The Pomodoro Technique is a popular time management method that involves breaking work into intervals (usually 25 minutes) separated by short breaks. In this blog post, we'll create a simple Pomodoro Timer application called TaskTracker using C# and .NET Core on Ubuntu. You can perform this on Windows with the same steps. The application will run in the console, displaying messages when it's time to work and when it's time to take a break.

Prerequisites:

Before we start, make sure you have .NET Core SDK installed on your Ubuntu machine. If you don't, follow the instructions at https://docs.microsoft.com/en-us/dotnet/core/install/linux-ubuntu to install it.

Step 1: Create a new console application

Open a terminal and type the following command to create a new console application:

dotnet new console -o TaskTracker
cd TaskTracker
Enter fullscreen mode Exit fullscreen mode

Step 2: Edit Program.cs

Open the Program.cs file in your favorite text editor or IDE. The file should look like this:

using System;

namespace TaskTracker
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a Timer class

Create a new file named Timer.cs in the TaskTracker folder and add the following code:

// Timer.cs
using System;
using System.Timers;

namespace TaskTracker
{
    public class Timer
    {
        private readonly int _interval;
        private readonly Timer _timer;

        public event EventHandler TimerFinished;

        public Timer(int interval)
        {
            _interval = interval;
            _timer = new Timer(_interval * 1000);
            _timer.Elapsed += Timer_Elapsed;
        }

        public void Start() => _timer.Start();

        public void Stop() => _timer.Stop();

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            _timer.Stop();
            TimerFinished?.Invoke(this, EventArgs.Empty);
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

The Timer class defines a custom timer that triggers an event when the specified interval elapses. We'll use this class to manage work and break intervals.

Step 4: Create a Task class

Create a new file named Task.cs in the TaskTracker folder and add the following code:

using System;

namespace TaskTracker
{
    public class Task
    {
        private readonly Timer _workTimer;
        private readonly Timer _breakTimer;
        private int _completedCycles;

        public Task(int workMinutes, int breakMinutes)
        {
            _workTimer = new Timer(workMinutes);
            _breakTimer = new Timer(breakMinutes);

            _workTimer.TimerFinished += WorkTimerFinished;
            _breakTimer.TimerFinished += BreakTimerFinished;
        }

        public void Start() => _workTimer.Start();

        private void WorkTimerFinished(object sender, EventArgs e)
        {
            Console.WriteLine("Work time is over. Take a break!");
            _breakTimer.Start();
        }

        private void BreakTimerFinished(object sender, EventArgs e)
        {
            _completedCycles++;
            Console.WriteLine($"Break time is over. Completed cycles: {_completedCycles}. Time to work!");
            _workTimer.Start();
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

The Task class manages work and break cycles using two instances of the Timer class. It starts the work timer and, when it finishes, starts the break timer. This cycle repeats indefinitely.

Step 5: Modify Program.cs

Update Program.cs to use the Task class:

// Program.cs
using System;

namespace PomodoroTimer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Pomodoro Timer!");

            int workMinutes = 25;
            int breakMinutes = 5;

            var pomodoro = new Pomodoro(workMinutes, breakMinutes);
            pomodoro.Start();
            Console.WriteLine("Press any key to exit the timer.");
Console.ReadKey();
}
}
}

Enter fullscreen mode Exit fullscreen mode

Step 6: Build and run the application

In the terminal, navigate to the TaskTracker folder and execute the following commands:

dotnet build
dotnet run
Enter fullscreen mode Exit fullscreen mode

The application will start, and the Task timer will begin with the default 25 minutes of work time and 5 minutes of break time. You'll see console messages indicating when it's time to work and when it's time to take a break. Press any key to exit the application.

Conclusion:

In this blog post, we created a simple C# TaskTracker application using .NET Core on Ubuntu. The application uses a custom Timer class to manage work and break intervals and a Pomodoro class to manage cycles. The console application provides a minimalistic interface to help users stay focused and manage their time effectively using the Pomodoro Technique.

Top comments (0)