如何在 WinForms 应用程序中取消 PLINQ 查询
我正在开发处理大量文本数据的应用程序,收集单词出现的统计信息(请参阅:源代码词云 )。
这是我的代码的简化核心正在做什么。
- 枚举所有扩展名为 *.txt 的文件。
- 枚举每个文本文件中的单词。
- 按单词分组并计算出现次数。
- 按出现次数排序。
- 输出前 20 名。
使用 LINQ 一切正常。迁移到 PLINQ 给我带来了显着的性能提升。 但是......长时间运行的查询期间的可取消性会丢失。
看来 OrderBy 查询正在将数据同步回主线程,并且 Windows 消息未得到处理。
在下面的示例中,我将根据 MSDN 如何:取消取消来演示取消的实现PLINQ 查询 不起作用:(
还有其他想法吗?
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();
}
}
}
}
}
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.
- Enumerate through all files with *.txt extension.
- Enumerate through words in each text files.
- Group by word and count occurrences.
- Sort by occurrences.
- 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();
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 Jon 所说,您需要在后台线程上启动 PLINQ 操作。这样,用户界面在等待操作完成之前不会挂起(因此可以调用“取消”按钮的事件处理程序并调用取消令牌的
Cancel
方法)。当令牌被取消时,PLINQ 查询会自动取消,因此您无需担心这一点。这是执行此操作的一种方法:
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:
您当前正在 UI 线程中迭代查询结果。即使查询是并行执行的,您仍然会在 UI 线程中迭代结果。这意味着 UI 线程太忙于执行计算(或等待查询从其他线程获取结果),无法响应“取消”按钮的单击。
您需要将迭代查询结果的工作转移到后台线程上。
You're currently iterating over the query results in the UI thread. Even though the query is executing in parallel, you're still iterating over the results in the UI thread. That means the UI thread is too busy performing computations (or waiting for the query to get results from its other threads) to respond to the click on the "Cancel" button.
You need to punt the work of iterating over the query results onto a background thread.
我想我找到了一些优雅的解决方案,它更适合 LINQ / PLINQ 概念。
我正在声明一个扩展方法。
而不是将其添加到我想要响应的任何地方的查询中。
效果很好!
请参阅我的帖子以获取更多信息和源代码:“如果可以的话请取消我”或 PLINQ 可取消性 & WinForms 中的响应能力
I think I found some elegant solution, which fits better in LINQ / PLINQ concept.
I am declaring an extension method.
And than adding it to my query wherever I want to be responsive.
It works fine!
See my post on it for more info and source code to play with: “Cancel me if you can” or PLINQ cancelability & responsiveness in WinForms