我正在处理 C# 中的文本文件列表。由于性能原因,我为此使用了 Parallel.ForEach。我需要richTextBox1在我的表单上写信,正在处理哪个文件。
由于我无法从 Parallel.ForEach 的不同线程访问表单,因此我WriteToBox使用 Invoke 创建了该方法。
public partial class Form2 : Form
{
private static Form2 frmReference;
private delegate void SafeCallDelegate(string text)
public Form2()
{
InitializeComponent();
frmReference = this;
}
public Dictionary<string, HashSet<string>> FindStrings(List<string> rccFiles)
{
Dictionary<string, HashSet<string>> sensitiveData = new Dictionary<string, HashSet<string>>();
Parallel.ForEach(rccFiles, rccFile =>
{
//write which file is beeing processed
WriteToBox("Searching in: " + rccFile);
//some code
});
return sensitiveData;
}
public void WriteToBox(string text)
{
if (frmReference.richTextBox1.InvokeRequired)
{
var d = new SafeCallDelegate(WriteToBox);
Invoke(d, new object[] { text });
}
else
{
frmReference.richTextBox1.AppendText("\r\n" + text);
frmReference.richTextBox1.Update();
frmReference.richTextBox1.ScrollToCaret();
}
}
}
当我运行程序时,它只会写入第一个处理的文件并且表单冻结(死锁?)。您知道如何从并行循环写入文本框吗?感谢您的任何建议!问候,马丁