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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:28:47+00:00 2026-05-12T17:28:47+00:00

I have an object that requires a lot of initialization (1-2 seconds on a

  • 0

I have an object that requires a lot of initialization (1-2 seconds on a beefy machine). Though once it is initialized it only takes about 20 miliseconds to do a typical “job”

In order to prevent it from being re-initialized every time an app wants to use it (which could be 50 times a second or not at all for minutes in typical usage), I decided to give it a job que, and have it run on its own thread, checking to see if there is any work for it in the que. However I’m not entirely sure how to make a thread that runs indefinetly with or without work.

Here’s what I have so far, any critique is welcomed

    private void DoWork()
    {
        while (true)
        {
            if (JobQue.Count > 0)
            {
                // do work on JobQue.Dequeue()
            }
            else
            {
                System.Threading.Thread.Sleep(50);
            }
        }
    }

After thought: I was thinking I may need to kill this thread gracefully insead of letting it run forever, so I think I will add a Job type that tells the thread to end. Any thoughts on how to end a thread like this also appreciated.

  • 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-12T17:28:48+00:00Added an answer on May 12, 2026 at 5:28 pm

    You need to lock anyway, so you can Wait and Pulse:

    while(true) {
        SomeType item;
        lock(queue) {
            while(queue.Count == 0) {
                Monitor.Wait(queue); // releases lock, waits for a Pulse,
                                     // and re-acquires the lock
            }
            item = queue.Dequeue(); // we have the lock, and there's data
        }
        // process item **outside** of the lock
    }
    

    with add like:

    lock(queue) {
        queue.Enqueue(item);
        // if the queue was empty, the worker may be waiting - wake it up
        if(queue.Count == 1) { Monitor.PulseAll(queue); }
    }
    

    You might also want to look at this question, which limits the size of the queue (blocking if it is too full).

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

Sidebar

Related Questions

I have an object that requires the URL of a file that resides in
I have a Javascript object that requires 2 calls out to an external server
I have a program that fundamentally requires a lot of memory. However, for some
We have an application that requires loading A LOT of configuration data at startup.
In java if you have an object that requires a generic to extends a
I have a singleton object that use another object (not singleton), to require some
I have two tables: object that has object_id column and avalues that have object_id
I have an object that hasn't been created yet (so it has no ID),
I have an object that I instantiate - it's quite nifty as it also
I have an object that is basically a Python implementation of an Oracle sequence.

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.