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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:42:36+00:00 2026-06-10T14:42:36+00:00

I have a thread that uses TcpClient ‘s BeginAcceptTcpClient() method. It resembles: // this

  • 0

I have a thread that uses TcpClient‘s BeginAcceptTcpClient() method. It resembles:

// this gets set elsewhere
ManualResetEvent _doneListening = new ManualResetEvent(false); 

.. and in the thread procedure

while (true)
{
    var result = BeginAcceptTcpClient(null, null);

    var index = WaitHandle.WaitAny(new [] { _doneListening, result.AsyncWaitHandle });
    if (0 == index)
    {
        break;
    }

    // do something with EndAcceptTcpClient(result)
}

Now, according to MSDN’s documentation:

The asynchronous BeginAcceptTcpClient operation must be completed by
calling the EndAcceptTcpClient method.

My question relates to the Cancel/Stop case of my thread — that is, after _doneListening.Set()— I really don’t want any more TcpClients. At the same time, I don’t want to leak memory.

Is it necessary to call EndAcceptTcpClient()? This will block, and I certainly don’t want it to (I want a fast thread exit). But will it leak memory otherwise? MSDN hints that Begin MUST be paired with an End. Is there a different pattern I should follow for effectively cancelling a potentially in-progress async operation?

  • 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-10T14:42:38+00:00Added an answer on June 10, 2026 at 2:42 pm

    EndAcceptTcpClient won’t block if you’ve closed/disposed the TcpClient. So when you are done listening then close it. It will throw an exception in response to EndAcceptTcpClient, but that is OK. You can safely ignore/discard the exception.

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

Sidebar

Related Questions

I have a tcp server that uses TCPListener and the async method BeginAcceptTCPClient: Imports
Using Java 6: I have a method that uses a Thread to run a
I have one thread that is receiving data over a socket like this: while
* So I have a Thread that holds a sleep method and I think
I have a thread function that looks something like this: DWORD WINAPI Thread_ProcessFile( LPVOID
We have a very simple method that uses findById. public Cart getCart(long cartId) {
I have a extension that uses multithreading using worker thread as shown here .
I have a multi-thread C# application that uses some recursive functions in a dll.
I have a thread that uses a handler to post a runnable instance. it
Hi I have a thread that uses couple of variables. variables are determined in

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.