How to make an Alarm clock in C#

By FoxLearn 11/21/2024 3:08:05 PM   13.81K
To create a simple alarm clock using the Timer control in C#, you can use Windows Forms and a Timer control that checks the system time against a specified alarm time.

When the current time matches the alarm time, the alarm will trigger.

How to make an Alarm clock in C#?

Open Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "AlarmClock" and then click OK

Drag and drop the DateTimePicker, Lable, Button controls from Visual Toolbox onto your form designer, then design your form as shown below.

c# alarm clock

Add code to button click event handler as below

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AlarmClock
{
    public partial class Form1 : Form
    {
        // Initialize the alarm time
        System.Timers.Timer timer;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Set up the timer to tick every second
            timer = new System.Timers.Timer();
            timer.Interval = 1000;
            timer.Elapsed += Timer_Elapsed;
        }

        // This method runs every time the timer ticks
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            DateTime currentTime = DateTime.Now;
            DateTime userTime = dateTimePicker.Value;
            // Check if current time matches the alarm time
            if (currentTime.Hour == userTime.Hour && currentTime.Minute == userTime.Minute && currentTime.Second == userTime.Second)
            {
                timer.Stop();
                UpdateLable upd = UpdateDataLable;
                //Update label control in mutiple thread
                if (lblStatus.InvokeRequired)
                    Invoke(upd, lblStatus, "Stop"); // Ensures thread-safe updates to the label control in a multi-threaded environment.
                // Trigger the alarm (play a sound, show a message, etc.)
                MessageBox.Show("Ring ring ring...", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        //Update label control
        delegate void UpdateLable(Label lbl, string value);
        void UpdateDataLable(Label lbl,string value)
        {
            lbl.Text = value;
        }

        // Starts the timer and updates the label to "Running...".
        private void btnStart_Click(object sender, EventArgs e)
        {
            timer.Start();
            lblStatus.Text = "Running...";
        }

        // Stops the timer and updates the label to "Stop".
        private void btnStop_Click(object sender, EventArgs e)
        {
            timer.Stop();
            lblStatus.Text = "Stop";
        }
    }
}

Every time the timer ticks, the current time is compared with the time set by the user (via a DateTimePicker).

If the current time matches the alarm time (hour, minute, second), the timer stops, and an alarm message box is shown with the text "Ring ring ring...".

The status label (lblStatus) is updated to "Stop" using thread-safe invocation (Invoke), ensuring the UI is updated properly from the timer's separate thread.

A delegate (UpdateLable) is used to update the label (lblStatus) in a thread-safe manner. The Invoke method ensures the label is updated on the UI thread.

This example implements an alarm clock using a System.Timers.Timer to check if the current time matches the alarm time set by the user.

VIDEO TUTORIAL