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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:10:29+00:00 2026-05-23T00:10:29+00:00

I currently have an application where I create a series of Tasks that execute

  • 0

I currently have an application where I create a series of Tasks that execute one after the other, with a cancellation source that can interrupt execution between Tasks (i.e. at safe termination points.) I currently use this cancellation source only when the managing class is Disposed, as a way to cleanly and quickly abort execution.

I now have a use case where I’d like to create an automatic timeout to cancel the Task sequence, in case the operator doesn’t respond in a timely manner (some of the Tasks wait on operator interaction with a physical mechanism.) At the same time, I still need to support cancellation if the managing class is Disposed. I found CancellationTokenSource.CreateLinkedTokenSource, which sounds like what I need, but I have a few concerns:

  1. Multiple Task-series can be executed in parallel, so I need to create a new CancellationTokenSource for timeout cancellation and an associated linked source for each Task-series that I begin. CancellationTokenSource implements IDisposable, which means that I need to persist both cancellation sources and Dispose them when the last Task completes or when any of the sub-Tasks is Cancelled or Faulted. This seems rather awkward, even with the helpful magic of anonymous method closures (still have these cancellation sources being passed around.)

  2. I also need to protect against the condition where the cancellation sources are Disposed before the timer expires (so I don’t Cancel a Disposed source.) This is a potential race condition, so I need to add appropriate locking. This also seems awkward, adds significant complexity (future maintenance cost) and makes unit testing more challenging (race conditions are tricky to reliably induce.)

Am I heading down the correct path here or is there a simpler way to do this?

  • 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-23T00:10:29+00:00Added an answer on May 23, 2026 at 12:10 am

    The core issue in your question seems to be relating to calling Dispose() on your Task and/or CancellationTokenSource objects. In this case, I would recommend just not calling Dispose on these, which should dramatically simplify your design.

    As justification I’ll refer you to this thread. In particular, Stephen Toub (the PM in charge of Task) suggests that you should:

    dispose aggressively if it’s easy and correct to do based on the structure of your code. If you start having to do strange gyrations in order to Dispose (or in the case of Tasks, use additional synchronization to ensure it’s safe to dispose, since Dispose may only be used once a task has completed), it’s likely better to rely on finalization to take care of things.

    This sounds like the exact situation he’s describing at the end – you’re trying to do strange gyrations in order to call Dispose() on these objects.

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

Sidebar

Related Questions

I'm currently looking into unit testing for a new application I have to create.
I currently have an application that gets hit with over 20,000 users daily and
Currently I have an application that receives an uploaded file from my web application.
I currently have an MS Access application that connects to a PostgreSQL database via
We currently have a quite complex business application that contains a huge lot of
I'm developing an application which currently have hundreds of objects created. Is it possible
I currently have an application which needs to have an awareness of which monitor
We currently have developed an application using WCF. Our clients make connections to different
I currently have heavily multi-threaded server application, and I'm shopping around for a good
I currently have a functioning in-house Windows Forms application which extensively uses the DataGridView

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.