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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:26:09+00:00 2026-05-17T06:26:09+00:00

I am interested to get some ideas from you about what would be a

  • 0

I am interested to get some ideas from you about what would be a good/better threading architecture that respects the rules described below:

  • A thread must be running for the life of the application, in the sleep/wait mode if there is no work in the queue to be performed.

  • A thread must be of a BelowNormal priority (this eliminates possibility of using ThreadPool).

  • The thread must give its feedback to the main thread upon completion of task.

  • Thread will monitor a Queue<T> to get more jobs to be performed.

I am using .Net Framework 4.0

Let me know what you think 🙂

  • 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-17T06:26:09+00:00Added an answer on May 17, 2026 at 6:26 am

    When I need to implement my own multi-threaded processing, I usually use something like this:

    public class MyWorker<T> : IDisposable
    {
        private readonly Queue<T> _taskQueue; // task queue
        private readonly object _threadLock = new object();
        private Thread _thread; // worker thread
        private ManualResetEvent _evExit;
        private AutoResetEvent _evNewData;
    
        /// <summary>Override this to process data.</summary>
        protected abstract void ProcessData(T data);
    
        /// <summary>Override this to set other thread priority.</summary>
        protected virtual ThreadPriority ThreadPriority
        {
            get { return ThreadPriority.BelowNormal; }
        }
    
        protected MyWorker()
        {
            _taskQueue = new Queue<T>();
            _evExit = new ManualResetEvent(false);
            _evNewData = new AutoResetEvent(false);
        }
    
        ~MyWorker()
        {
            Dispose(false);
        }
    
        private void ThreadProc()
        {
            try
            {
                var wh = new WaitHandle[] { _evExit, _evNewData };
                while(true)
                {
                    T data = default(T);
                    bool gotData = false;
                    lock(_taskQueue) // sync
                    {
                        if(_taskQueue.Count != 0) // have data?
                        {
                            data = _taskQueue.Dequeue();
                            gotData = true;
                        }
                    }
                    if(!gotData)
                    {
                        if(WaitHandle.WaitAny(wh) == 0) return; // demanded stop
                        continue; //we have data now, grab it
                    }
                    ProcessData(data);
                    if(_evExit.WaitOne(0)) return;
                }
            }
            catch(ThreadInterruptedException)
            {
                // log warning - this is not normal
            }
            catch(ThreadAbortException)
            {
                // log warning - this is not normal
            }
        }
    
        public void Start()
        {
            lock(_threadLock)
            {
                if(_thread != null)
                    throw new InvalidOperationException("Already running.");
                _thread = new Thread(ThreadProc)
                {
                    Name = "Worker Thread",
                    IsBackground = true,
                    Priority = ThreadPriority,
                };
                _thread.Start();
            }
        }
    
        public void Stop()
        {
            lock(_threadLock)
            {
                if(_thread == null)
                    throw new InvalidOperationException("Is not running.");
                _evExit.Set();
                if(!_thread.Join(1000))
                    _thread.Abort();
                _thread = null;
            }
        }
    
        /// <summary>Enqueue data for processing.</summary>
        public void EnqueueData(T data)
        {
            lock(_taskQueue)
            {
                _taskQueue.Enqueue(data);
                _evNewData.Set(); // wake thread if it is sleeping
            }
        }
    
        /// <summary>Clear all pending data processing requests.</summary>
        public void ClearData()
        {
            lock(_taskQueue)
            {
                _taskQueue.Clear();
                _evNewData.Reset();
            }
        }
    
        protected virtual void Dispose(bool disposing)
        {
            lock(_threadLock)
            {
                if(_thread != null)
                {
                    _evExit.Set();
                    if(!_thread.Join(1000))
                        _thread.Abort();
                    _thread = null;
                }
            }
            _evExit.Close();
            _evNewData.Close();
            if(disposing)
                _taskQueue.Clear();
        }
    
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm curious to get some feedback and ideas on how one could go about
I am trying to get started doing some Silverlight development. I am interested in
I'm interested as sometimes I get friends who are developers telling me about their
I am interested to get the topic headings (say all lines with Heading 1
When setting up a UITableView's data source, I'd be interested to get views on
I am interested in where string literals get allocated/stored. I did find one intriguing
Background I am interested in getting to grips with some Ruby On Rails. I've
my fellow developers! I hope very much that at least some of you will
I've been doing about an hour of research on this, coming from zero graphics
I have been writing some code that detects add and removal of USB devices,

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.