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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:10:29+00:00 2026-05-28T06:10:29+00:00

What I have is something similar to this: //method being called by thread pool

  • 0

What I have is something similar to this:

//method being called by thread pool thread
public string someFunction(){
    string someString = "string";

    //Stuff happens

    //Need to wait for 5 seconds without blocking thread

    return someString;
}

The issue is that I need the result of that method returned to the method that called it, however, I do not want it returned immediately and I do not want to block the thread. Is there a way that I can make the thread pause at that line for a specified amount of time and release the thread back into the thread pool, and then after some timeout, one of the thread pool threads picks up where it left off and completes the function and returns?


All… After spending some time with your responses, I realize now that doing what I am trying to achieve is not so much possible in the context in which I want it.

A little explanation though. I am trying to create a rudimentary long poll web server in c sharp for a web based chat application I am building. I was wanting to have the thread enter a method where it waits until one of two things happens, either data shows up for the client, or a poll timeout occurs. However, I did not want the thread to block, I have some time in the past implemented something where each client got a thread and the thread blocked and trust me… it is NOT a good idea. The 5 seconds in the example was arbitrary and the actual time will likely sit somewhere between 1 and 5 minutes. In the end, the structure I may end up going with is a sort of client management thread. a thread that works its way through a list asking each client if the client has work to be done. If the client has reached its timeout or has data waiting in its queue, then the appropriate method gets dispatched into the thread pool and and the Client Management thread continues on to the next client.

Something like so…

while(true){
    foreach(Client client in ClientList){
        //check if the client has something it needs done
        if(client.needsWork){
               //invoke the appropriate method asynchronously
               delegate = client.appropriateInvokable;
               delegate.beginInvoke();
        }
    }
}

Thank you all considerably for your help and patience!

  • 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-28T06:10:29+00:00Added an answer on May 28, 2026 at 6:10 am

    After looking through your question, I see it’s a bit trickier than just getting a timer callback on a new thread. The real issue is that your call is structured to return a value synchronously, so there is no way you can avoid blocking the calling thread. The correct way to avoid blocking is to change your call to use an asynchronous callback rather than returning the value directly. If you have access to the TPL, one nice way to do this is to return a Task object that the caller can then Wait on immediately or register for a callback.


    That said, for the timing callback aspect, I have some utility code that I use to simplify working with System.Threading.Timer in these one-off timer scenarios. I’ve cut it down to the specified functionality and put it below:

    public sealed class TimerService
    {
        /// <summary>
        ///   This method registers a call back to be called after a specified period of time.
        /// </summary>
        /// <param name = "duration">The duration after which to call back</param>
        /// <param name = "callback">The method to call back</param>
        public void WhenElapsed(TimeSpan duration, Action callback)
        {
            if (callback == null) throw new ArgumentNullException("callback");
    
            //Set up state to allow cleanup after timer completes
            var timerState = new TimerState(callback);
            var timer = new Timer(OnTimerElapsed, timerState, Timeout.Infinite, Timeout.Infinite);
            timerState.Timer = timer;
    
            //Start the timer
            timer.Change((int)duration.TotalMilliseconds, Timeout.Infinite);
        }
    
        private void OnTimerElapsed(Object state)
        {
            var timerState = (TimerState)state;
            timerState.Timer.Dispose();
            timerState.Callback();
        }
    
        private sealed class TimerState
        {
            public TimerState(Action callback)
            {
                Callback = callback;
            }
    
            public Timer Timer { get; set; }
    
            public Action Callback { get; private set; }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have something similar to the following method: public ActionResult Details(int id) { var
The short question: Does Castle Windsor have something similar to Spring.Net's Lookup Method Injection
I have something similar to that: <table> <tr> <td>Hello,<br/>World!</td> </tr> </table> Both lines Hello,
i have something similar to the following... used a python script to JSON to
I have already posted something similar here but I would like to ask the
We currently have an ant task that contains something similar to the following: <filelist
i have something that requires a matrix of values, similar to pokemon: (source: firechao.com
I have used getopt in Python and was hoping there would be something similar
I have a view that I'm trying to filter with something similar to DISTINCT
I have an object with event handlers and I want to make something similar

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.