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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:36:04+00:00 2026-05-27T05:36:04+00:00

I’m developing a class library that implements a protocol stack. There I have an

  • 0

I’m developing a class library that implements a protocol stack. There I have an asynchronous operation DoSomething that implements IAsyncResult. The user of this method can use BeginDoSomething and EndDoSomething. So long it’s quite a common task.

Now I got the requirement to make the asynchronous operation abortable. I can think of several ways to achieve this, but I’m not sure which one is the best:

  1. Have a CancelDoSomething method besides BeginDoSomething and EndDoSomething. This is not the common asaync pattern and thus not easy to find out for the user of the operation.
  2. According to no. 1: Have a Cancel property
  3. The instance of IAsyncResult that is returned by BeginDoSomething can be casted to DoSomethingAsyncResult. DoSomethingAsyncResult has a Cancel method. Again, this is not the common usage of the async pattern and hardly to know for the “common user” of the class library.
  4. According to no. 3: Have a Cancel property
  5. The instance of IAsyncResult that is returned by BeginDoSomething implements IAbortableAsyncResult: public IAbortableAsyncResult:IAsyncResult { void Abort(); }. For me this seems to be the most elegant way. When the user starts the operation using BeginDoSomething he gets an instance of IAbortableAsyncResult which means that this special feature is prominently introduced to him.

What is the best solution? Are there even more ways to solve this? I prefer no. 5 because it doesn’t have a code smell ( at least I didn’t recognize until now).

[EDIT]

An approach not relying on TPL/.NET 4 is appreciated to have a solution that is also applicable in legacy code that cannot be ported to the newest version of .NET framework.

[/EDIT]

  • 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-27T05:36:05+00:00Added an answer on May 27, 2026 at 5:36 am

    CancellationToken and CancellationTokenSource were added to .net in 4.0, and form the basis for a common cancellation pattern ever since. Generally, asynchronous methods have an overload which takes a CancellationToken.

    Consumers of CancellationTokens may register for a callback when the token is canceled, and use the callback to stop executing. Alternatively, if the task is not particularly long-executing, the IsCanceled property can just be polled intermittently.

    The CancellationToken is the interface used by the asynchronous task, the initiator of the task uses a CancellationTokenSource to issue this token, and may then cancel it in the future.

    One of the nice things about this approach, other than it becoming a standard way to approach cancellation, is that it is composable. If the task composes several other cancellable asynchronous operations, the same cancellation token can be passed through to them and the cancellation should automatically work itself out.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but

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.