如何在 WinForms 应用程序中取消 PLINQ 查询

发布于 2024-11-27 22:33:00 字数 2627 浏览 0 评论 0原文

我正在开发处理大量文本数据的应用程序,收集单词出现的统计信息(请参阅:源代码词云 )。

这是我的代码的简化核心正在做什么。

  1. 枚举所有扩展名为 *.txt 的文件。
  2. 枚举每个文本文件中的单词。
  3. 按单词分组并计算出现次数。
  4. 按出现次数排序。
  5. 输出前 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.

  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();
                }
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半寸时光 2024-12-04 22:33:00

正如 Jon 所说,您需要在后台线程上启动 PLINQ 操作。这样,用户界面在等待操作完成之前不会挂起(因此可以调用“取消”按钮的事件处理程序并调用取消令牌的 Cancel 方法)。当令牌被取消时,PLINQ 查询会自动取消,因此您无需担心这一点。

这是执行此操作的一种方法:

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);
      }
    }));
  });
}

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);
      }
    }));
  });
}
两个我 2024-12-04 22:33:00

您当前正在 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.

音盲 2024-12-04 22:33:00

我想我找到了一些优雅的解决方案,它更适合 LINQ / PLINQ 概念。

我正在声明一个扩展方法。

public static class ProcessWindowsMessagesExtension
{
    public static ParallelQuery<TSource> DoEvents<TSource>(this ParallelQuery<TSource> source)
    {
        return source.Select(
            item =>
            {
                Application.DoEvents();
                Thread.Yield();
                return item;
            });
    }
}

而不是将其添加到我想要响应的任何地方的查询中。

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

效果很好!

请参阅我的帖子以获取更多信息和源代码:“如果可以的话请取消我”或 PLINQ 可取消性 & WinForms 中的响应能力

I think I found some elegant solution, which fits better in LINQ / PLINQ concept.

I am declaring an extension method.

public static class ProcessWindowsMessagesExtension
{
    public static ParallelQuery<TSource> DoEvents<TSource>(this ParallelQuery<TSource> source)
    {
        return source.Select(
            item =>
            {
                Application.DoEvents();
                Thread.Yield();
                return item;
            });
    }
}

And than adding it to my query wherever I want to be responsive.

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文