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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:07:24+00:00 2026-05-13T09:07:24+00:00

I want to write my first real MultiThreaded C# Application. While I used a

  • 0

I want to write my first real MultiThreaded C# Application. While I used a BackgroundWorker before and know a thing or two about lock(object), I never used the Thread object, Monitor.Enter etc. and I’m completely lost where to start designing the Architecture.

Essentially my program runs in the background. Every 5 Minutes, it checks a web service. If the web service returns data, it creates Jobs out of this data and passes it into a JobQueue. The JobQueue then sequentially works on those jobs – if a new job is added while it still is working on one, it will queue the job. Additionally, there is a Web Server to allow remote access to the program.

The way I see it, I need 4 Threads:

  1. The Main Thread
  2. The “5-Minute-Timer” and WebService Thread
  3. The JobQueue
  4. The Web Server

Thread 2-4 should be created when the program launches and ended when the program ends, so they only run once.

As said, i don’t really know how the architecture would work on that. What would Thread 1 do? When the MyProgram class is instantiated, should it have a Queue<Job> as a Property? How would I start my Thread? As far as I see, I need to pass in a Function into the Thread – where should that function sit? If I have a class “MyJobQueueThreadClass” that has all the functions for Thread 3, how would that access an Object on the MyProgram class? And if a Thread is just a function, how do I prevent it from ending early? As said, Thread 2 waits 5 Minutes, then executes a series of functions, and restarts the 5 minute timer (Thread.Sleep(300)?) over and over again, until my Program is ended (Call Thread.Abort(Thread2) in the Close/Exit/Destructor of MyProgram?)

  • 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-13T09:07:25+00:00Added an answer on May 13, 2026 at 9:07 am

    Let’s go through it, step by step:

    1.

    class Program {
    

    The job queue is a data structure:

        private static Queue<Job> jobQueue;
    

    If this data structure is accessed by multiple threads, you need to lock it:

        private static void EnqueueJob(Job job) {
            lock (jobQueue) {
                jobQueue.Enqueue(job);
            }
        }
    
        private static Job DequeueJob() {
            lock (jobQueue) {
                return jobQueue.Dequeue();
            }
        }
    

    Let’s add a method that retrieves a job from the web service and adds it to the queue:

        private static void RetrieveJob(object unused) {
            Job job = ... // retrieve job from webservice
            EnqueueJob(job);
        }
    

    And a method that processes jobs in the queue in a loop:

        private static void ProcessJobs() {
            while (true) {
                Job job = DequeueJob();
                // process job
            }
        }
    

    Let’s run this program:

        private static void Main() {
            // run RetrieveJob every 5 minutes using a timer
            Timer timer = new Timer(RetrieveJob);
            timer.Change(TimeSpan.FromMinutes(0), TimeSpan.FromMinutes(5));
    
            // run ProcessJobs in thread
            Thread thread = new Thread(ProcessJobs);
            thread.Start();
    
            // block main thread
            Console.ReadLine();
        }
    }
    

    2.

    If you run the program, you’ll notice that a job is added every 5 minutes. But jobQueue.Dequeue() will throw an InvalidOperationException because the job queue is empty until a job is retrieved.

    To fix that, we turn the job queue into a blocking queue by using a Semaphore:

        private static Semaphore semaphore = new Semaphore(0, int.MaxValue);
    
        private static void EnqueueJob(Job job) {
            lock (jobQueue) {
                jobQueue.Enqueue(job);
            }
            // signal availability of job
            semaphore.Release(1);
        }
    
        private static Job DequeueJob() {
            // wait until job is available
            semaphore.WaitOne();
            lock (jobQueue) {
                return jobQueue.Dequeue();
            }
        }
    

    3.

    If you run the program again, it won’t throw the exception and everything should work fine. But you’ll notice that you have to kill the process because the ProcessJobs-thread never ends. So, how to you end your program?

    I recommend you define a special job that indicates the end of job processing:

        private static void ProcessJobs() {
            while (true) {
                Job job = DequeueJob();
                if (job == null) {
                    break;
                }
                // process job
            }
            // when ProcessJobs returns, the thread ends
        }
    

    Then stop the timer and add the special job to the job queue:

        private static void Main() {
            // run RetrieveJob every 5 minutes using a timer
            Timer timer = new Timer(RetrieveJob);
            timer.Change(TimeSpan.FromMinutes(0), TimeSpan.FromMinutes(5));
    
            // run ProcessJobs in thread
            Thread thread = new Thread(ProcessJobs);
            thread.Start();
    
            // block main thread
            Console.ReadLine();
    
            // stop the timer
            timer.Change(Timeout.Infinite, Timeout.Infinite);
    
            // add 'null' job and wait until ProcessJobs has finished
            EnqueueJob(null);
            thread.Join();
        }
    

    I hope this implicitly answers all your questions 🙂

    Rules of thumb

    • Start a thread by specifying a method that has access to all necessary data structures

      • Use ThreadPool.QueueUserWorkItem for small tasks
      • Use a Timer for small, periodic tasks
      • Use a Thread for long-running tasks
    • When accessing data structures from multiple threads, you need to lock the data structures

      • In most cases the lock statement will do
      • Use a ReaderWriterLockSlim if there are many threads reading from a data structure that is infrequently changed.
      • You don’t need a lock if the data structure is immutable.
    • When multiple threads depend on each other (e.g., a thread waiting for another thread to complete a task) use signals

      • ManualResetEvent, AutoResetEvent, Semaphore
      • Thread.Join if the task is waiting for the thread to end
    • Do not use Thread.Abort, Thread.Interrupt, Thread.Resume, Thread.Sleep, Thread.Suspend, Monitor.Pulse, Monitor.Wait

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

Sidebar

Ask A Question

Stats

  • Questions 300k
  • Answers 300k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Now that you've disabled the CSRF modules, you no longer… May 13, 2026 at 7:54 pm
  • Editorial Team
    Editorial Team added an answer Is there a way to invoke maven console from eclipse?… May 13, 2026 at 7:54 pm
  • Editorial Team
    Editorial Team added an answer Performance needs to be defined before it is measured. Is… May 13, 2026 at 7:54 pm

Related Questions

I am trying to write my first real python function that does something real.
First a little intro: Last year i wrote this http://dragan.yourtree.org/code/canvas-3d-graph/ Now, i want to
My first programming job introduced me to unit testing and the concept of mock
So I have some SMTP stuff in my code and I am trying to
Preface Let me start off by saying that I'm a relatively new programmer and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.