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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:01:42+00:00 2026-05-18T06:01:42+00:00

I have an program where I need to create some large number of folders

  • 0

I have an program where I need to create some large number of folders to an external sharepoint site (external meaning I can’t use the sharepoint object model). Web requests work well for this, but simply doing them one at a time (send request, wait for response, repeat) is rather slow. I decided to multithread the requests, to try and speed it up. The program has sped up considerably, but after some amount of time (between 1-2 minutes or so), concurrency exceptions start getting thrown.

Code is below, is this the best way to go about this?

Semaphore Lock = new Semaphore(10, 10);
List<string> folderPathList = new List<string>();
//folderPathList populated

foreach (string folderPath in folderPathList)
{
    Lock.WaitOne();
    new Thread(delegate()
    {
        WebRequest request = WebRequest.Create(folderPath);
        request.Credentials = DefaultCredentials;
        request.Method = "MKCOL";

        WebResponse response = request.GetResponse();
        response.Close();
        Lock.Release();
    }).Start();
}
for(int i = 1;i <= 10;i++)
{
    Lock.WaitOne();
}

The exception is something along the lines of

Unhandled Exception: System.Net.WebException: Unable to connect to the remote server —> System.Net.Sockets.SocketException: Only one useage of each socket address is normally permitted 192.0.0.1:81
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
ss socketAddress)
at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state,
IAsyncResult asyncResult, Int32 timeout, Exception& exception)

  • 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-18T06:01:42+00:00Added an answer on May 18, 2026 at 6:01 am

    You might create too many connections, thus using up all the local ports you can use.
    There’s a timeout period for when a port can be reused after you close it.
    WebRequest hides all the low level socket handling for you, but I am guessing it eventually runs out of ports, or tries to (re)bind to a socket already in a TIME_WAIT state.

    You should make sure you read the response stream, even if you don’t care about the response. This should help not producing too many lingering connections.

    WebResponse response = request.GetResponse();
    new StreamReader(response.GetResponseStream()).ReadToEnd(); 
    

    I’ll paste some relevant info from here:

    When a connection is closed, on the side that is closing the connection the 5 tuple
    { Protocol, Local IP, Local Port, Remote IP, Remote Port} goes into a TIME_WAIT state for 240 seconds by default.
    In this case, the protocol is fixed – TCP
    the local IP, remote IP and remote PORT are also typically fixed. So the variable is the local port.
    What happens is that when you don’t bind, a port in the range 1024-5000 is used.
    So roughly you have 4000 ports. If you use all of them in 4 minutes – meaning roughly you
    make 16 web service calls per second for 4 minutes you will exhaust all the ports. That is the cause of this exception.

    OK now how can this be fixed?

    1. One of the ways is to increase the dynamic port range. The max by default is 5000. You can set this up to 65534.
      HKLM\System\CurrentControlSet\Services\Tcpip\Parameters\MaxUserPort is the key to use.

    2. The second thing you can do is once the connection does get into an TIME_WAIT state you can reduce the time it is
      in that state, Default is 4 minutes, but you can set this to 30 seconds
      HKLM\System\CurrentControlSet\Services\Tcpip\Parameters\TCPTimedWaitDelay is the key to use.
      Set this to 30 seconds

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

Sidebar

Related Questions

I have a program in which I need to store a Class object into
I have program with asp.net c# but now i need to create an application
I need to create a very long string in a program, and have been
I need to execute some large file-based .sql scripts (to create/drop databases and tables
I need to create a virtual file, a file that if some program access
I have created a vector of sets in my program, and I need to
I have a program that I need to be able to search a file
I have a program for a C class I need to write. The program
I have a program (temptrack) where I need to download weather data every x
I have a legacy program that I need to run with the extension of

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.