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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:33:50+00:00 2026-05-27T01:33:50+00:00

Lets say I have a processor who’s job is to persist files back to

  • 0

Lets say I have a processor who’s job is to persist files back to the disk. This is running as a Task while observing a BlockingCollection<T> for files to process.

When the task gets cancelled and there are still files which should be saved to the disk, what would be a good practice for doing so?

It would be convenient to let the task right before exiting quickly write the files remaining back to the disk although I’m not sure if this conflicts with the philosophy of cancelling a task (since cancellation should happen as quick as possible).

Another option is having a second procedure after cancelling the task who’s job is to write the remaining files to the disk.

Code example:

class FileProcessor
{
    private readonly BlockingCollection<Stream> input;

    public FileProcessor(BlockingCollection<Stream> input)
    {
        _input = input;
    }

    public Task Run(CancellationToken cancellationToken, 
        BlockingCollection<Stream> input)
    {
        return Task.Factory.StartNew(() => 
        {
            foreach (Stream stream in 
                        input.GetConsumingEnumerable(cancellationToken))
            {
                WriteToDisk(stream);
            }

            // Should I call WriteRemaining here or should I have
                    // a process running after this task exited which 
                    // will call WriteRemaining
            WriteRemaining();
        });
    }

    public void WriteRemaining()
    {
        foreach (Stream stream in input)    
        {
            WriteToDisk(stream);
        }
    }
}

I know this is a bit of an open question, the application/requirements/amount of files to write also play a role but I’m seeking for the general guideline/best practices here.

  • 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-27T01:33:51+00:00Added an answer on May 27, 2026 at 1:33 am

    Cancellation is a cooperative action when working with the Task Parallel Library, and yes, cancelling is recommended to be a quick operation.

    Remember, this is a cancellation, not a cancellation and cleanup. If you have extra operations that you need to perform as the result of a cancellation, then those operations should occur outside of the original task that was cancelled.

    Note that this doesn’t stop you from calling ContinueWith and performing an operation in a new Task which checks to see if the IsCanceled property returns true and then performs the cleanup based on that.

    The key point here is that you don’t want to block the original Task that was cancelled, but you are free to start a new Task to perform whatever cleanup you need to do as a result of the cancellation.

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

Sidebar

Related Questions

Lets say i have a thread running like this: private boolean working = true;
lets say I have a process with the ID 1234. This process is running
Lets say have this immutable record type: public class Record { public Record(int x,
Lets say I have this code: <?php class hello { var $greeting = hello;
Lets say I have hierarchy like this (This is just a test program. Please
Lets say I have one table called REVIEWS This table has Reviews that customers
Let's say we have a very processor-intensive task at hand which could be effectively
Lets say you have some functions in some classes are called together like this
Lets say I have 100 servers each running a daemon - lets call it
Let's say i have 15 processes/daemons running in the background. let's say one of

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.