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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:26:18+00:00 2026-05-20T02:26:18+00:00

I am about to start a C# .NET 4.0 project that creates a job

  • 0

I am about to start a C# .NET 4.0 project that creates a job scheduler.

  1. The job has no due date, and are potentially long running, up to days.
  2. The job has 3 priorities: idle, normal, critical; from lowest to highest.
  3. New job(s) are continuously being created.
  4. Newer job with higher priority should take precedence over lower priority job even if the old job has been created for a long time.
  5. Each job will be handled by single long running thread.
  6. Job are re-entrant. The state of job is persisted to the database, so it is okay to pause the job or terminate the job thread at anytime.

My plan is to use a semaphore, and set the number of concurrent entries to the number of system cores. A new thread will be created for every jobs in queue, and all thread will be blocked by the semaphore in the beginning.

My problem is to guarantee high priority thread will enter the semaphore first when semaphore calls release() method. Doable?

My second problem is to have a thread that is inside the semaphore to exit when higher priority job thread come to existence and have the exiting job thread go back to the thread queue to wait for the semaphore. Doable?

For those two problems, is semaphore the right approaches? If not what do you suggest?

  • 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-20T02:26:18+00:00Added an answer on May 20, 2026 at 2:26 am

    Well, I would lean more towards something like the following…

    First, Start all the threads you want:

    for(int i=0; i < Environment.ProcessorCount; i++)
    {
        Thread t = new Thread(RunWork);
        // setup thread
        t.Start();
        threads.Add(t);
    }
    

    You will need an interface to describe the priority of a task

    interface ITask {
        PrioirtyType Prioirty { get; }
        bool Complete { get; }
        void PerformOneUnitOfWork();
    }
    

    Then create a Queue management object. This would obviously get more complicated as it may need to sync with your database, etc…

    class MyQueue<TJob> where TJob : ITask 
    {
        Queue<TJob> high, med, low;
        bool GetNextJob(ref TJob work)
        {
            if(work.Priority == PriorityType.High && !work.Complete)
                return true;
            lock(this)
            {
                if(high.Count > 0)
                {
                    Enqueue(work);//requeue to pick back up later
                    work = high.Dequeue();
                    return true;
                }
                if(work.Priority == PriorityType.Med && !work.Complete)
                    return true;
                if(med.Count > 0)
                {
                    Enqueue(work);//requeue to pick back up later
                    work = med.Dequeue();
                    return true;
                }
                if(!work.Complete)
                    return true;
                if(low.Count > 0)
                {
                    work = low.Dequeue();
                    return true;
                }
                work = null;
                return false;
            }
    
        void Enqueue(TJob work)
        {
            if(work.Complete) return;
            lock(this)
            {
                else if(work.Priority == PriorityType.High) high.Enqueue(work);
                else if(work.Priority == PriorityType.Med) med.Enqueue(work);
                else low.Enqueue(work);
            }
        }
    }
    

    And Lastly create your worker thread something like the following:

    public void RunWork()
    {
        ITask job;
        while(!_shutdown.WaitOne(0))
        {
            if(queue.GetNextJob(ref job))
                job.PerformOneUnitOfWork();
            else
                WaitHandle.WaitAny(new WaitHandle[] { _shutdown, queue.WorkReadyHandle });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose, I am about to start a project using ASP.NET and SQL Server 2005.
In .Net, I think about a web service as being a project type that
I am about to start implementing the data access infrastructure of a project that
I'm new to the .net framework. I'm about to start a project where I
I'm about to start a project that will record and edit audio files, and
I'm about to start a project that requires XML messages to be sent between
I'm about to start building a new asp.net project, and I'm just starting out
I am about to start a new application and am excited about using asp.net
I am about to start a project for university to build a procedural city
I am about to start a new personal project. It aims to be 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.