Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6784109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:57:00+00:00 2026-05-26T16:57:00+00:00

I’m creating a countdown timer that allows the user to choose a valid time,

  • 0

I’m creating a countdown timer that allows the user to choose a valid time, and once the countdown reaches zero, it plays a sound.

I’ve instantiated a timer from the System.Timers namespace and set its Interval to 1 second. I convert the user-specified time to seconds, and decrement that value by 1 every time the Timer.Elapsed function hits. Once it reaches zero, that means the countdown has reached zero which means it’s time to play the sound.

However, whenever it doesn’t reach zero, I decrement the time value by 1 and I also want to increment the ProgressBar by using the progressbar.Increment function.

Unfortunately, whenever I do this, it gives me an exception having to do with multithreading issues. I know what is wrong, but I am not sure how to fix it. Do I need to start the timer.Elapsed function on a new thread?

The error is:

Cross-thread operation not valid: Control ‘CountdownProgress’ accessed
from a thread other than the thread it was created on.

Also, any tips on better programming habits are welcome as well.
Many thanks!

Here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;
using System.Media;
using System.Diagnostics;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form_CDA : Form
    {
        public Form_CDA()
        {
            InitializeComponent();
        }

        private bool m_CountdownActive; // declare global variables and instantiate timer.
        private bool m_Cancelled;
        public decimal seconds;
        public decimal minutes;
        public decimal hours;
        public decimal time;
        public System.Timers.Timer timer = new System.Timers.Timer();

        private void Form_CDA_Load(object sender, EventArgs e)
        {
            m_Cancelled = false;
            timer.AutoReset = false;
            timer.Interval = 0;
            m_CountdownActive = false;
            richTextBox1.Text = "Hello, please select an hour between 0 and 100, a minute between 0 and 59, and a second between 0 and 59. The countdown timer will play a sound when finished.";
            btn_Cancel.Enabled = false;
            seconds = 0;
            minutes = 0;
            hours = 0;
            time = 0;
            m_StatusBar.Text = "Program properly loaded, waiting for user input"; // initialize variables.
        }

        private void btn_SetCountdown_Click(object sender, EventArgs e)
        {
            seconds = numUpDown_Seconds.Value;
            minutes = numUpDown_Minutes.Value;
            hours = numUpDown_Hours.Value;
            time = (hours * 3600) + (minutes * 60) + seconds; // calculate the total time in seconds.
            if (time != 0) // if time is not zero, start timer and set up event handler timer.elapsed.
            {
                timer.Interval = 1000;
                timer.AutoReset = true;
                timer.Start();
                timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
                CountdownProgress.Maximum = (int)time;
                CountdownProgress.Minimum = 0;
                CountdownProgress.Value = 0;
            }
            else
            {
                m_StatusBar.Text = "Invalid selection of times. Try again.";
                return;
            }
            DateTime dt = DateTime.Now;
            dt.AddSeconds((double)time);
            Label_Countdown.Text = "Finishing time: " + dt.ToString(); // display end time to user.
            m_CountdownActive = true;
            btn_Cancel.Enabled = true;
            btn_SetCountdown.Enabled = false;
            numUpDown_Hours.Enabled = false;
            numUpDown_Minutes.Enabled = false;
            numUpDown_Seconds.Enabled = false; // disable controls.
            m_Cancelled = true;
            m_StatusBar.Text = "Timer set to " + numUpDown_Hours.Value.ToString() + " hours, " + numUpDown_Minutes.Value.ToString() + " minutes, " + numUpDown_Seconds.Value.ToString() + " seconds.";
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (time == 0)
            {
                m_StatusBar.Text = "Countdown Finished";
                SoundPlayer soundPlayer = new SoundPlayer(@"C:\Users\marupakuuu\Desktop\New stuff\doorbell.wav");
                soundPlayer.Play(); // play sound.
                timer.Stop();
                return;
            }
            else
            {
                time = time - 1;
                CountdownProgress.Increment(1); // exception occurs here; multithreading issues.
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            // if user wishes to stop the countdown to start a new one.
            m_Cancelled = true;
            m_CountdownActive = false;
            btn_SetCountdown.Enabled = true;
            numUpDown_Seconds.Value = 0;
            numUpDown_Minutes.Value = 0;
            numUpDown_Hours.Value = 0;
            numUpDown_Hours.Enabled = true;
            numUpDown_Minutes.Enabled = true;
            numUpDown_Seconds.Enabled = true;
            btn_Cancel.Enabled = false;
            m_StatusBar.Text = "Countdown cancelled";
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:57:01+00:00Added an answer on May 26, 2026 at 4:57 pm

    A control can only be accessed within the thread it was created. Therefore use Invoke to execute the given code as a delegate on the main thread, where the control (progressbar) was created.

    void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        if (time == 0)
        {
            m_StatusBar.Text = "Countdown Finished";
            SoundPlayer soundPlayer = new SoundPlayer(@"C:\Users\marupakuuu\Desktop\New stuff\doorbell.wav");
            soundPlayer.Play(); // play sound.
            timer.Stop();
            return;
        }
        else
        {
            time = time - 1;
            Invoke(new Action(() => CountdownProgress.Increment(1)));
        }
    }
    

    You may want to read http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.