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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:56:50+00:00 2026-05-15T15:56:50+00:00

I have the scenario where a command comes in over a socket which requires

  • 0

I have the scenario where a command comes in over a socket which requires a fair amount of work. Only one thread can process the data at a time. The commands will come in faster than can process it. Over time there will be quiet a back log.

The good part is that I can discard waiting threads and really only have to process the last one that is waiting – (or process the first one in and discard all the other once). I was thinking about using a semaphore to control the critical section of code and to use a boolean to see if there are any threads blocking. If there are blocking thread I would just discard the thread.

My mind is drawing a blank on how to implement it nicely I would like to implement it with out using an integer or boolean to see if there is a thread waiting already.

I am coding this in c#

  • 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-15T15:56:51+00:00Added an answer on May 15, 2026 at 3:56 pm

    You can use Monitor.TryEnter to see whether a lock is already taken on an object:

    void ProcessConnection(TcpClient client)
    {
        bool lockTaken = false;
    
        Monitor.TryEnter(lockObject, out lockTaken);
    
        if (!lockTaken)
        {
            client.Close();
            return;
        }
    
        try
        {
            // long-running process here
        }
        finally
        {
            Monitor.Exit(lockObject);
            client.Close();
        }
    }
    

    Note that for this to work you’ll still have to invoke the method in a thread, for example:

    client = listener.AcceptTcpClient();
    ThreadPool.QueueUserWorkItem(notused => ProcessConnection(client));
    

    FYI, the lock statement is just sugar for:

    Monitor.Enter(lockObject);
    
    try
    {
        // code within lock { }
    }
    finally
    {
        Monitor.Exit(lockObject);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a scenario in which I have to run multiple commands one after
I have a scenario where combobox can have same string values. for exa combo
Scenario: I have one JFrame and JPanel added to that JFrame . on this
Scenario: I have a php page in which I call a python script. Python
I have the following scenario: My app gets some data from the command line.
I have a scenario in which I need to execute the below statement. air
Scenario: I have local uncommitted changes to a source file which I want to
I have the following scenario for command line argument. If there is a particular
I have a scenario where one PHP process is writing a file about 3
I have a simple scenario and a issue with it which I just cant

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.