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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:33:31+00:00 2026-06-09T14:33:31+00:00

Say I create a task bool aBoolean = true; Task.Factory.StartNew(() => while(aBooloean) { …

  • 0

Say I create a task

bool aBoolean = true;

Task.Factory.StartNew(() =>
    while(aBooloean)    {
       ...
    }
}

Is it better to exit the task by having a global boolean to exit the Task?

Or ist it better to use CancellationTokenSource?

When is it appropriate to use a CancellationTokenSource to end a Task?

Does it just throw an exception when you use CancellationTokenSource and leave things in a inconsistent state?

Thanks

  • 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-09T14:33:32+00:00Added an answer on June 9, 2026 at 2:33 pm

    Having a “global” boolean runs the risk of being optimized or cached into CPU cache and you may not see a change in value. CancellationTokenSource is the recommended method for cancelling a Task. The Task uses the Token to test if cancellation is requested and simply returns from the Task’s Action delegate. No need to throw an exception if you don’t want to.

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

Sidebar

Related Questions

is there a way to create a task/activity report (say a weekly report) off
I want to create a program so that what I say, Add Task. it
Say I create one object and add it to my ArrayList . If I
Say you create a directory like: CREATE OR REPLACE DIRECTORY EXT_DATA_FILES AS '/data/ext_data_files'; GRANT
Say I create some object class like so public class thing { private String
Let's say I create a new Entity and Save it as follows: UserReport report
Let's say I create an instance of a class and want to assign some
Let's say I create a jagged 2d array like so: public static char[,] phoneLetterMapping
Just starting to study the Azure framework. Just say you create a row in
Let's say that I create a vector/array in Scheme: (let* ((x #(1 2 3)))

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.