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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:57:54+00:00 2026-06-09T11:57:54+00:00

i am trying to build a multi threaded server that is supposed to spawn

  • 0

i am trying to build a multi threaded server that is supposed to spawn new threads for every incoming connection, BUT for all my effort, it spawns new threads only when it feels like it, Can anybody help me debug this code? am i missing something obvious?

 while (true)
 {
   if (txtAddress.Text.Trim() == "Any" || txtAddress.Text.Trim() == "any")
      ipEndP = new IPEndPoint(IPAddress.Any, 778);
   else
      ipEndP = new IPEndPoint(IPAddress.Parse(txtAddress.Text.Trim()), 778);

      tcpL = new TcpListener(ipEndP);
      tcpL.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

      tcpL.Start();
      tempSock = tcpL.AcceptSocket();

 //t = new Thread(ConnectionHandler); //When new client is connected, new thread       //is created to handle the connection
 //t.Priority = ThreadPriority.Highest;
 //t.Start(tempSock);

 ThreadPool.QueueUserWorkItem(ConnectionHandler, tempSock);
 }
  • 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-06-09T11:57:55+00:00Added an answer on June 9, 2026 at 11:57 am

    Check out the MSDN docs for QueueUserWorkItem

    Queues a method for execution. The method executes when a thread pool thread becomes available.

    Placing a thread in the user work item queue does not guarantee that it will begin executing right away. That’s actually a good thing. If you got so many connections that you needed hundreds or thousands of threads, that could easily bring your server to it’s knees (and certainly would be very wasteful due to excessive context switches).

    Your commented out code should kick off a new thread for every connection. Is that not working? If so, what exactly is not working? Note that creating a new thread for every connection is much more expensive than using the thread pool.

    UPDATE

    Based on your remark that the commented out code is also failing to create too many threads, I would add…

    You are creating WAY too many threads if that happens.

    Often I see people asking why they can’t create more than around 2000 threads in a process. The reason is not that there is any particular limit inherent in Windows. Rather, the programmer failed to take into account the amount of address space each thread uses.

    A thread consists of some memory in kernel mode (kernel stacks and object management), some memory in user mode (the thread environment block, thread-local storage, that sort of thing), plus its stack. (Or stacks if you’re on an Itanium system.)

    Usually, the limiting factor is the stack size.

    http://blogs.msdn.com/b/oldnewthing/archive/2005/07/29/444912.aspx

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

Sidebar

Related Questions

I am trying to build a multi-page form with Yii, but am quite new
I'm trying to figure out how to build a multi-dimensional array that is: flexible
I'm trying to build a server that will handle many concurrent connections to iPhone
I am trying to build a multi-app CodeIgniter site, where the assets for all
I'm trying to build a multi model form - but one of the issues,
I understand that .NET is multi-threaded and that is a good thing, but I
I'm trying to build a multi-column layout that is adaptive (fluid, elastic, whatever you
Hello I am trying to build a multi select to populate a many-to-many join
I was trying Build For Archiving application (from Titanium Mobile) with xCode 4.4, but
I'm trying build a MVC framework, but I'm confused about manage themes. Well... I

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.