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

  • Home
  • SEARCH
  • 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 992483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:16:31+00:00 2026-05-16T06:16:31+00:00

How could I modify the below code such that I could, for better readability

  • 0

How could I modify the below code such that I could, for better readability in code:

a) move the “workThreadMethod()” into it’s own class

b) not have any code in this worker thread class reference static variables from the main “Program” class

c) the above are the main two requirements, however I’m hoping that as a side effect this would then ensure that for testability the worker thread class methods would be easier to test and ideally lend itself to testing via the IOC (e.g. Ninject) concept [if this doesn’t make sense then ignore this point for the purpose of the question]

The main challenge that I’m not sure about re solving is how to handle the two different shared variables between the originating thread & new thread (one of them a ConcurrentQueue which the new thread adds to, and the other a bool variable that the original thread uses to indicate to the new thread when to stop)

using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading;

namespace TestConsoleApp
{
    class Program
    {
        // Main Thread uses to indicate to New Thread to stop
        private static bool _shouldStop = false;

        // New Thread uses to pass back result to Main Thread
        private static long _results = 0;

        // Main Thread passes ongoing updates to New Thread via this queue
        private static ConcurrentQueue<long> _workQueue = new ConcurrentQueue<long>();

        static void Main(string[] args)
        {
            var p = new Program();
            p.TestThreads();
        }

        public void TestThreads()
        {
            _shouldStop = false;
            var workThread = new Thread(workThreadMethod);
            workThread.Start();

            for (int i = 0; i < 100; i++)
            {
                _workQueue.Enqueue(i);   // Add test data to queue
                Debug.WriteLine("Queue  : " + i);
                Thread.Sleep(10);
            }

            Thread.Sleep(5000);

            _shouldStop = true;
            workThread.Join();
            Debug.WriteLine("Finished TestThreads.  Result = " + _results);
        }


        // Dequeuer Methods
        private void workThreadMethod()
        {
            // Update Summary
            while (!_shouldStop)
            {
                if (_workQueue.Count == 0)
                {
                    Thread.Sleep(10);
                }
                else
                {
                    long currentValue;
                    bool worked = _workQueue.TryDequeue(out currentValue);
                    if (worked)
                    {
                        _results += currentValue;
                        Debug.WriteLine("DeQueue: " + currentValue);
                    }
                }
            }
        }
    }
}
  • 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-16T06:16:32+00:00Added an answer on May 16, 2026 at 6:16 am

    This is an exercise in separation of concerns, and initially I would split this program into a work provider and worker. The provider is responsible for the queue and execution control while the worker should do calculation. The following code is a crude start but it should get you going.

    Splitting up the two concerns and using constructor injection already pays of in testability, you can now fully test Worker without involving Program.

    Note: considering further development of your app I would strongly suggest that you look into the Task Parallel Library. Using a library such as the TPL enables you to take advantage of multi-core processors without having to deal with the complexities of thread allocation and work scheduling. More resources on the TPL is discussed here.

    public interface IWorkProvider
    {
        bool ShouldStop { get; }
        long? GetWork();
    }
    
    public class Program : IWorkProvider
    {
        // Main Thread uses to indicate to New Thread to stop
        private static bool _shouldStop = false;
    
        // Main Thread passes ongoing updates to New Thread via this queue
        private static ConcurrentQueue<long> _workQueue = new ConcurrentQueue<long>();
    
        public bool ShouldStop { get { return _shouldStop; } }
    
        public long? GetWork()
        {
            long currentValue;
            bool worked = _workQueue.TryDequeue(out currentValue);
            if (worked)
                return currentValue;
            return null;
        }
    }
    
    public class Worker
    {
        private long _results;
        private readonly IWorkProvider _workProvider;
    
        public long Results { get { return _results; }}
    
        public Worker(IWorkProvider workProvider)
        {
            _workProvider = workProvider;
        }
    
        public void DoWork()
        {
            // Update Summary
            while (!_workProvider.ShouldStop)
            {
                long? work = _workProvider.GetWork();
                if (work.HasValue)
                {
                    _results += work.Value;
                    Debug.WriteLine("DeQueue: " + work.Value);
                }
                else
                {
                    Thread.Sleep(10);
                }
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the demonstrator code below that fails to compile with the error: Occurs
I'm wondering how to modify my below code so that I may determine if
Question summary: How do I modify the code below so that untrusted, dynamically-loaded code
With VCL, we had a TRadioGroup class with an Items property you could modify
Is there a common interface in Python that I could derive from to modify
I have a setup that looks like this. class Checker { // member data
I have been tweaking with below sample code. The documentation for MathJax isn't very
could you please clarify my issue? I have a starting page ListBox1.aspx that calls
I have the css code below along with an image to show it's output.
I have two related qestions on the code included below 1) I am trying

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.