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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:58:53+00:00 2026-05-22T17:58:53+00:00

I have used CancellationTokenSource to provide a function so that the user can cancel

  • 0

I have used CancellationTokenSource to provide a function so that the user can
cancel the lengthy action. However, after the user applies the first cancellation,
the later further action doesn’t work anymore. My guess is that the status of CancellationTokenSource has been set to Cancel and I want to know how to reset
it back.

  • Question 1: How to reset the CancellationTokenSource after the first time usage?

  • Question 2: How to debug the multithread in VS2010?
    If I run the application in debug mode, I can see the following exception for
    the statement

    this.Text = string.Format("Processing {0} on thread {1}", filename, Thread.CurrentThread.ManagedThreadId);
    

InvalidOperaationException was unhandled by user code
Cross-thread operation not valid: Control ‘MainForm’ accessed from a thread other
than the thread it was created on.

Thank you.

private CancellationTokenSource cancelToken = new CancellationTokenSource();

private void button1_Click(object sender, EventArgs e)
{
    Task.Factory.StartNew( () =>
    {
        ProcessFilesThree();
    });
}

private void ProcessFilesThree()
{
    ParallelOptions parOpts = new ParallelOptions();
    parOpts.CancellationToken = cancelToken.Token;
    parOpts.MaxDegreeOfParallelism = System.Environment.ProcessorCount;

    string[] files = Directory.GetFiles(@"C:\temp\In", "*.jpg", SearchOption.AllDirectories);
    string newDir = @"C:\temp\Out\";
    Directory.CreateDirectory(newDir);

    try
    {
        Parallel.ForEach(files, parOpts, (currentFile) =>
        {
            parOpts.CancellationToken.ThrowIfCancellationRequested();

            string filename = Path.GetFileName(currentFile);

            using (Bitmap bitmap = new Bitmap(currentFile))
            {
                bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
                bitmap.Save(Path.Combine(newDir, filename));
                this.Text =  tring.Format("Processing {0} on thread {1}",  filename, Thread.CurrentThread.ManagedThreadId);
            }
        });

        this.Text = "All done!";
    }
    catch (OperationCanceledException ex)
    {
        this.Text = ex.Message;                             
    }
}

private void button2_Click(object sender, EventArgs e)
{
    cancelToken.Cancel();
}
  • 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-22T17:58:53+00:00Added an answer on May 22, 2026 at 5:58 pm

    Question 1> How to reset the CancellationTokenSource after the first time usage?

    If you cancel it, then it’s cancelled and can’t be restored. You need a new CancellationTokenSource. A CancellationTokenSource isn’t some kind of factory. It’s just the owner of a single token. IMO it should have been called CancellationTokenOwner.

    Question 2> How to debug the multithread in VS2010? If I run the application in debug mode, I can see the following exception for the statement

    That has nothing to do with debugging. You can’t access a gui control from another thread. You need to use Invoke for that. I guess you see the problem only in debug mode because some checks are disabled in release mode. But the bug is still there.

    Parallel.ForEach(files, parOpts, (currentFile) =>
    {
      ...  
      this.Text =  ...;// <- this assignment is illegal
      ...
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used checkbox column in gridview. I want to check status of that
I have used ASP.NET Web Services (.asmx) in the past. However, I have heard
I have used split function to split the string in javascript. which is as,
I have used the smtp class to send emails through code. Can I use
I have used ValidatorEnable to disable a RequiredFieldValidator in javascript. However on postback the
We have used Custom List View inside that one Text View and two Edit
I have used Shibboleth with manual login, meaning the user clicks the login link
I have used alias ruby=ruby1.9.1, so I can execute my ruby with this: ruby
I have used gallery in my app. In that i have two images in
I have used Rhino.Mocks extensively currently writing some tests in Java using EasyMocks. However

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.