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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:18:49+00:00 2026-05-24T06:18:49+00:00

I am working on application which processes large amount of text data gathering statistics

  • 0

I am working on application which processes large amount of text data gathering statistics on word occurrences (see: Source Code Word Cloud).

Here what the simplified core of my code is doing.

  1. Enumerate through all files with *.txt extension.
  2. Enumerate through words in each text files.
  3. Group by word and count occurrences.
  4. Sort by occurrences.
  5. Output top 20.

Everything worked fine with LINQ. Moving to PLINQ brought me significant performance boost.
But … cancelability during long running queries is lost.

It seems that the OrderBy Query is synchronizing data back into main thread and windows messages are not processed.

In the examle below I am demonstarting my implementation of cancelation according to MSDN How to: Cancel a PLINQ Query whic does not work 🙁

Any other ideas?

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace PlinqCancelability
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            m_CancellationTokenSource = new CancellationTokenSource();
        }

        private readonly CancellationTokenSource m_CancellationTokenSource;

        private void buttonStart_Click(object sender, EventArgs e)
        {
            var result = Directory
                .EnumerateFiles(@"c:\temp", "*.txt", SearchOption.AllDirectories)
                .AsParallel()
                .WithCancellation(m_CancellationTokenSource.Token)
                .SelectMany(File.ReadLines)
                .SelectMany(ReadWords)
                .GroupBy(word => word, (word, words) => new Tuple<int, string>(words.Count(), word))
                .OrderByDescending(occurrencesWordPair => occurrencesWordPair.Item1)
                .Take(20);

            try
            {
                foreach (Tuple<int, string> tuple in result)
                {
                    Console.WriteLine(tuple);
                }
            }
            catch (OperationCanceledException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            m_CancellationTokenSource.Cancel();
        }

        private static IEnumerable<string> ReadWords(string line)
        {
            StringBuilder word = new StringBuilder();
            foreach (char ch in line)
            {
                if (char.IsLetter(ch))
                {
                    word.Append(ch);
                }
                else
                {
                    if (word.Length != 0) continue;
                    yield return word.ToString();
                    word.Clear();
                }
            }
        }
    }
}
  • 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-24T06:18:51+00:00Added an answer on May 24, 2026 at 6:18 am

    As Jon said, you’ll need to start the PLINQ operation on a background thread. This way, the user interface doesn’t hang while waiting until the operation completes (so the event handler for Cancel button can be invoked and the Cancel method of the cancellation token gets called). The PLINQ query cancels itself automatically when the token is cancelled, so you don’t need to worry about that.

    Here is one way to do this:

    private void buttonStart_Click(object sender, EventArgs e)
    {
      // Starts a task that runs the operation (on background thread)
      // Note: I added 'ToList' so that the result is actually evaluated
      // and all results are stored in an in-memory data structure.
      var task = Task.Factory.StartNew(() =>
        Directory
            .EnumerateFiles(@"c:\temp", "*.txt", SearchOption.AllDirectories)
            .AsParallel()
            .WithCancellation(m_CancellationTokenSource.Token)
            .SelectMany(File.ReadLines)
            .SelectMany(ReadWords)
            .GroupBy(word => word, (word, words) => 
                new Tuple<int, string>(words.Count(), word))
            .OrderByDescending(occurrencesWordPair => occurrencesWordPair.Item1)
            .Take(20).ToList(), m_CancellationTokenSource.Token);
    
      // Specify what happens when the task completes
      // Use 'this.Invoke' to specify that the operation happens on GUI thread
      // (where you can safely access GUI elements of your WinForms app)
      task.ContinueWith(res => {
        this.Invoke(new Action(() => {
          try
          {
            foreach (Tuple<int, string> tuple in res.Result)
            {
              Console.WriteLine(tuple);
            }
          }
          catch (OperationCanceledException ex)
          {
              Console.WriteLine(ex.Message);
          }
        }));
      });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an application which reads in a huge amount of data
I'm working on an application which stores data in tables, similar to an RDBMS.
I'm working on an application which has data imported from a foreign (and wholly
I am currently working on an application that launches separate processes which display additional
I am working on a very large application that has multiple processes running simultaneously;
I am working on an application which has a large array containing lines of
I am working on an application which draws a simple dot grid. I would
I am working on an application which starts as a service but only if
I am working on an application which sends an AJAX POST request (I'm using
I'm working on iphone application which needs to run offline at an exhibition. It

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.