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 4120700
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:15:21+00:00 2026-05-20T23:15:21+00:00

I have never had to use threads much – only a few occasions. But

  • 0

I have never had to use threads much – only a few occasions. But today, I was bored, and wanted to play with them… and try build an understanding. It looks like BackgroundWorkerThread is a good thing… So, I tried to make a console app that simply writes ‘Tick’ ever 5 seconds, 5 times. And this is what I came up with:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            BackgroundWorker bw = new BackgroundWorker();

            AlarmClock ac = new AlarmClock(5);
            bw.DoWork += ac.StartAlarm;
            bw.RunWorkerAsync(5);

            bool run = true;

            while(run)
            {
                run = bw.IsBusy;  
            }
            Console.WriteLine("Finished!!");
            Console.WriteLine("Press a key...");
            Console.ReadKey();
        }
    }

    public class AlarmClock
    {
        private int noOfTicks;

        public AlarmClock (int noOfTicks)
        {
            this.noOfTicks = noOfTicks;
        }

        public void StartAlarm(object sender, DoWorkEventArgs e)
        {
            DateTime start = DateTime.Now;
            Console.WriteLine("Alarm set to tick ever 5 seconds.");
            int ticks = 0;
            bool runMe = true;

            while (runMe)
            {
                if (DateTime.Now.Second % 5 == 0)
                {
                    Console.WriteLine("Tick..");
                    ticks++;
                    Thread.Sleep(1000);
                }
                runMe = ticks < noOfTicks;
            }
            Console.WriteLine("Aboring thread.");
        }

    }
}

But it seems messy. Can anyone assist me to show me how it SHOULD be done?

  • 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-20T23:15:22+00:00Added an answer on May 20, 2026 at 11:15 pm

    As answered above, for the described case Timer is more efficient than BackgroundWorker. But if your goal is to learn how to work with BackgroundWorker class, you can do something like this:

    class Program
    {
        static void Main()
        {
            var worker = new BackgroundWorker {WorkerReportsProgress = true};
            worker.DoWork += DoWork;
            worker.ProgressChanged += ReportProgress;
            worker.RunWorkerAsync(5);
    
            Console.ReadKey();
        }
    
        private static void DoWork(object sender, DoWorkEventArgs e)
        {
            int count = (int) e.Argument;
            for (int i = 1; i <= count; i++)
            {
                (sender as BackgroundWorker).ReportProgress(i);
                Thread.Sleep(5000); // Do your work
            }
        }
    
        private static void ReportProgress(object sender, ProgressChangedEventArgs e)
        {
            Console.WriteLine("Tick " + e.ProgressPercentage);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a basic cs-major understanding of multi-threading but have never had to do
I've worked with mysql for some time but have never had to do anything
I started learning how to use Selenium today. I have never used it before.
OK, another road bump in my current project. I have never had form elements
As a mostly self-taught programmer, I have never really had anyone explain why certain
We have a few Tcl/Tk scripts (because a GUI we use can incorporate macros
Hey all! I'm fairly new to objective-c so I have never had to tackle
I have never worked with web services and rails, and obviously this is something
I have never hand-coded object creation code for SQL Server and foreign key decleration
I have never used octal numbers in my code nor come across any code

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.