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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:36:15+00:00 2026-05-25T20:36:15+00:00

In this particular case, I’m writing a TCP/IP server, and I’m using TcpListener.BeginAcceptTcpClient() to

  • 0

In this particular case, I’m writing a TCP/IP server, and I’m using TcpListener.BeginAcceptTcpClient() to accept incoming connections. The server logics is implemented in a single class implementing the IDisposable interface: when Dispose() is called, I want to cancel the BeginAcceptTcpClient() operation.

Now in more general terms: when using the async I/O model of .NET (Begin* and End*), how can I cancel the operations?

My idea would be something like this:

private volatile bool canceledFlag = false;

this.listener.BeginAcceptTcpClient(asyncResult =>
{
    if(this.canceledFlag) return;

    // ...
}, null);

Dispose() would set the flag to true and would also call this.listener.Stop().
However I recall having read that for every Begin* call there must be a matching End* call or bad things would happen.

So how should I do this then? Please note that I’m looking for a general solution for cancellation with Begin* and End* methods – I just gave you my concrete usage scenario to help you understand my question.

  • 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-25T20:36:16+00:00Added an answer on May 25, 2026 at 8:36 pm

    The general solution is to call Close() or Dispose() on whatever object whose BeginXxxx() method you called. That will cause the callback to run, when you call EndXxxx() then you’ll get an ObjectDisposedException. Which you should catch and treat as a ‘end of use’ signal, clean up and exit the callback right away. While frowned upon, the use of an exception for flow control is unavoidable.

    For TcpListener, use Server.Dispose(), a bit more efficient than calling Stop().

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

Sidebar

Related Questions

I'm using a detailsview control to update a record, however in this particular case
I've been trying to avoid using a cursor in this particular case just because
I'm still not sure I totally get how this particular case should work out.
This is more a general question but my particular case involves a ruby/rails app
I am having some speed issue with my datatables. In this particular case I
In general, I want warnings of unsigned vs signed. However, in this particular case,
I'm having a trouble since jQuery.ajax, in this particular case, doesn't seem to happen,
i'm constructing a web-service that is used, in this particular case, to ask for
I'm trying to execute some methods (in this particular case, rdOnAllDone) from a third
I have a collection with an index on :created_at (which in this particular case

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.