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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:21:08+00:00 2026-06-09T00:21:08+00:00

I have a Windows form with three buttons. One button adds entries to a

  • 0

I have a Windows form with three buttons. One button adds entries to a BlockingCollection. One starts processing the list and one stops processing the list.

I can add entries to my BlockingCollection and when I click Start, the list is consumed as I would expect. I can still add new items and they continue to be consumed. However when I click the Stop button, although the tasks DO stop, I cannot start them again with the start button.

What am I doing wrong in the cancellation of the tasks, that they won’t start again? I’ve read countless items on cancelling tasks but still don’t ‘get it’.

Any help would be great. Here’s the code…

    // Blocking list for thread safe queuing
    private BlockingCollection<QueueItem> ItemList = new BlockingCollection<QueueItem>();
    private CancellationTokenSource CancelTokenSource = new CancellationTokenSource();
    private int MaxConsumers = 3;

    // Form initialisation
    public MainForm()
    {
        InitializeComponent();
    }

    // Create an async consumer and add to managed list
    private void CreateConsumer(int iIdentifier)
    {
        Task consumer = Task.Factory.StartNew(() =>
        {
            foreach (QueueItem item in ItemList.GetConsumingEnumerable())
            {
                Console.WriteLine("Consumer " + iIdentifier.ToString() + ": PROCESSED " + item.DataName);
                Thread.Sleep(894);

                if (CancelTokenSource.IsCancellationRequested)
                    break; // Cancel request
            }
        }, CancelTokenSource.Token);
    }

    // Add a new item to the queue
    private void buttonAdd_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 10; i++)
        {
            Task.Factory.StartNew(() =>
            {
                // Add an item
                QueueItem newItem = new QueueItem(RandomString(12));

                // Add to blocking queue
                ItemList.Add(newItem);
            });
        }
    }

    // Start button clicked
    private void buttonStart_Click(object sender, EventArgs e)
    {
        buttonStart.Enabled = false;
        buttonStop.Enabled = true;

        // Create consumers
        for (int i = 0; i < MaxConsumers; i++)
        {
            CreateConsumer(i);
        }
    }

    // Stop button clicked
    private void buttonStop_Click(object sender, EventArgs e)
    {
        CancelTokenSource.Cancel();

        buttonStop.Enabled = false;
        buttonStart.Enabled = true;
    }

    // Generate random number
    private static Random random = new Random((int)DateTime.Now.Ticks);
    private string RandomString(int size)
    {
        StringBuilder builder = new StringBuilder();
        char ch;
        for (int i = 0; i < size; i++)
        {
            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            builder.Append(ch);
        }

        return builder.ToString();
    }
  • 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-09T00:21:09+00:00Added an answer on June 9, 2026 at 12:21 am

    You re-use the same cancellation token that is still configured to cancel.

    So create a new token when starting new tasks.

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

Sidebar

Related Questions

In VB.net 3.5 SP1 I have a Windows Form with three DataGridView controls. One
I have a windows form application. On the form there are three groupboxs. Each
I have a windows forms application in .NET 3.5. There's one form with 20
I have one windows form file that each time I open it in Visual
I have a data grid view in one windows form named GridViewForm. When the
I have developed a Windows application where a button is there on a form
Scenario I have a Windows Forms Application. Inside the main form there is a
I have a windows form app, and a class which is called Game. Let's
I have a windows form where in I open new threads for server communication.
i have a windows form that contains a bunch of textboxes. I need a

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.