Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Cancel a PLINQ Query in a WinForms Application
    primarykey
    data
    text
    <p>I am working on application which processes large amount of text data gathering statistics on word occurrences (see: <a href="http://sourcecodecloud.codeplex.com/" rel="nofollow">Source Code Word Cloud</a>).</p> <p>Here what the simplified core of my code is doing.</p> <ol> <li>Enumerate through all files with *.txt extension.</li> <li>Enumerate through words in each text files.</li> <li>Group by word and count occurrences.</li> <li>Sort by occurrences.</li> <li>Output top 20.</li> </ol> <p>Everything worked fine with LINQ. Moving to PLINQ brought me significant performance boost. But ... cancelability during long running queries is lost.</p> <p>It seems that the OrderBy Query is synchronizing data back into main thread and windows messages are not processed.</p> <p>In the examle below I am demonstarting my implementation of cancelation according to <a href="http://msdn.microsoft.com/en-us/library/dd460707.aspx" rel="nofollow">MSDN How to: Cancel a PLINQ Query</a> whic does not work :(</p> <p>Any other ideas?</p> <pre><code>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 =&gt; word, (word, words) =&gt; new Tuple&lt;int, string&gt;(words.Count(), word)) .OrderByDescending(occurrencesWordPair =&gt; occurrencesWordPair.Item1) .Take(20); try { foreach (Tuple&lt;int, string&gt; 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&lt;string&gt; 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(); } } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload