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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:46:32+00:00 2026-06-12T02:46:32+00:00

I have a background async Task that is fired from a button click. I

  • 0

I have a background async Task that is fired from a button click. I have an IsBusy property that is updated when the Task starts and within a finally block to account for cancellation or completion.

Currently my button click event cancels the previous Task and starts a new Task when it is re-clicked; however since the cancellation of the first Task may linger past the start of the new one, a boolean IsBusy indicator can become out of sync.

My issue is how to deal with this situation.

My current solution is not to update the boolean value directly but to use an integer that is incremented / decremented on start and completion – with the IsBusy property returning BusyCount > 0. This however requires additions for thread safety, and does not feel like a good solution.

Another thought was to store the Task when it is fired and only start the new task as a continuation of the first. This accounts for the cancellation issue, but forces a unnecessary delay (however fractional) and a ‘contract’ for the caller rather than the task.

My final thought was to create something like a BusyTokenSource class that hands out disposable BusyTokens that get added / removed from a thread-safe collection.. but this seems like overkill.

Is there a better/proper/common way to handle this type of situation?

Edit:

I ended up going with my last thought above, and wrote the ‘BusyToken’ classes. This way I can simply wrap a function in a using(BusyTokenSource.GetToken()){} and let the internal collection and Dispose take care of the counting and busy status.

  • 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-12T02:46:34+00:00Added an answer on June 12, 2026 at 2:46 am

    I think you should have one busy-status per Task. Not sure how your BusyTokenSource idea works but it sounds a little like it.

    Create a holder class:

    class BusyStatus { bool IsBusy; }
    

    And give a new instance of it to each Task you start. When you are no longer interested in the old task, just discard the old BusyStatus and create a new instance. The old Task will eventually finish and set the busy status, but on the old instance which nobody uses anymore.

    Just make sure that the old task does not accidentally reference the new BusyStatus instance.

    As a more general rule it is often useful to have each Task (or Thread) work on distinct data structures. This a useful general guiding principle. It gains you simplicity and makes it easy to achieve safety.

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

Sidebar

Related Questions

In my iPhone app I have a background task that starts running when the
I have a background worker thread that is constantly syncing data to/from a remote
I have an AsyncTask and on the background I want to start Service that
I have this background image that is 175x175 but I am trying to make
I have a background image that is 1024 x 1024 pixels; the reason for
We have a webservice function that runs a list of tasks in the background.
I have AsyncTask that processes some background HTTP stuff. AsyncTask runs on schedule (Alarms/service)
I have an activity that starts with an AsyncTask to check the connection to
I'm in the process of setting up a background task that makes network calls;
I have a long running task that I want completed, even if the application

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.