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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:05:02+00:00 2026-06-17T19:05:02+00:00

I am writing a program that has several Worker objects going off and doing

  • 0

I am writing a program that has several “Worker” objects going off and doing tasks that take as set amount of time. I have created a worker class with an internal timer that is working fine. However, when doing the “work” i will at times need to wait several seconds for a screen refresh (each worker is scraping data from a remote screen and doing some automation).

For those pauses, i don’t want to sleep the thread, because as i understand it that will also
pause the timers on the other worker objects (my application is a single thread because, frankly, I’m brand new to C# and i didn’t want to overreach). Is there another waiting function that i can use that doesn’t actually hang the whole thread?

Some additional info:

  • Right now this is a console app, but i will eventually be building a UI form to provide feedback to the user on how the workers are doing
  • My timers are implemented using System.Timers and are working quite nicely
  • I am brand new to C# programming, this is my first project, so please use small words 😉
  • Using MS VS Express 2012 for Desktop (so whatever version of C# / .NET that is!)

Code below (the actual work will be done using the “startWorking” method, but nothing is implemented – this is just my sold build with timers working. Also, the main is just being used for testing multiple timers right now)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace Multi_Timers
{
//worker class that includes a timer
public class Worker
{
    private Timer taskTimer;
    private bool available = true;
    private string workerName;
    private string startWork;
    private int workTime;

    // properties
    public bool isAvailable { get { return this.available; } }
    public string name { get { return this.workerName; } }

    // constructor
    public Worker(string name)
    {
        this.workerName = name;
        Console.WriteLine("{0} is initialized", name);
    }

    // start work timer
    public void startWorking(int duration) {

        if (this.available == true)
        {
            this.available = false;
            this.taskTimer = new Timer();
            this.taskTimer.Interval = duration;
            this.taskTimer.Elapsed += new ElapsedEventHandler(doneWorking);
            this.taskTimer.Enabled = true;
            this.startWork = DateTime.Now.ToString();
            this.workTime = duration / 1000;
        }
        else Console.WriteLine("Sorry, {0} was not available to work", this.workerName);
    }

    // Handler for timer
    public void doneWorking(object sender, ElapsedEventArgs e)
    {
        Console.WriteLine("{0}: {1} / {2} min / {3}", this.workerName, this.startWork, this.workTime/60, e.SignalTime.ToLocalTime());
        this.taskTimer.Enabled = false;

        this.available = true;

    }
}

//main program
class Program
{
    static void Main(string[] args)
    {
        Random r = new Random();

        // initialize worker(s)
        Worker bob = new Worker("Bob");
        Worker bill = new Worker("Bill");
        Worker jim = new Worker("Jim");

        // q to exit
        while (true)
        {
            if (bob.isAvailable) {

                bob.startWorking(r.Next(1 * 60, 150 * 60) * 1000);
            }
            if (bill.isAvailable)
            {
                bill.startWorking(r.Next(1 * 60, 150 * 60) * 1000);
            }
            if (jim.isAvailable)
            {
                jim.startWorking(r.Next(1 * 60, 150 * 60) * 1000);
            }

        }
    }
}
}

Thank you for any help in advance! Reading examples from this community was definitely key in teaching myself a little bit of C# to get started with!

  • 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-06-17T19:05:03+00:00Added an answer on June 17, 2026 at 7:05 pm

    i don’t want to sleep the thread, because as i understand it that will also pause the timers on the other worker objects

    That is incorrect; it will not stop the timers.

    my application is a single thread

    No, actually, it’s not. The timers will be creating and using other threads to implement their behavior. The Elapsed event handlers will be fired from a thread pool thread.


    One major issue with your code is that your main method is doing a “busywait” on the three objects, constantly polling them asking if they’re done. This is…expensive. It’s basically like you’re kids asking, “Are we there yet.” a few hundred times a second. Wouldn’t it be so much nicer if they just sat their waiting until you told them you were done! (That’s quite possible, and a good possible option.)

    One of the simpler solutions in this case would be to do the loop in the worker, not Main. Have the implementation of startWorking wrapped in a while loop, and have main just do a wait forever (i.e. Thread.Sleep(Timeout.Infinite);). More complex options would be having the workers provide a Task, event, or blocking wait (also called a “Join”) method to indicate when they are finished.

    The option you’re considering, that of adding a Thread.Sleep of a little while in Main will help, but it’s only telling your kids to ask you when you’re there less often, rather than having them wait for you to tell them when you’re there.

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

Sidebar

Related Questions

I'm writing a CPU intensive program in C++ that has several threads needing to
I am writing a program in Visual C# 2010 that has several icons on
I'm writing a ruby program that has 2 threads. One that listens on an
I'm writing a program that's parsing an XML file to java objects using smooks.
I'm writing a program that has an X11/Xlib interface, and my event processing loop
I'm writing a C++ program that has a large number of classes. In my
I am writing a program that has to copy a sizeable, but not huge
I am currently writing a program that has a function that needs to delete
I am currently writing a program that has a function that prints a linked
I am writing a program that connects to a web host that has a

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.