Parallel.ForEachが正常に動作するのに問題があります。私はrichtextboxにファイルの内容または複数のファイルの内容(各ファイルはログファイルを表す)を読み込むlistviewコントロールを持っています。 Foreachループで作業していたのですが、100以上のファイルをロードするときに並列処理によってロード時間が短縮されることになりました。Parallel.ForEachとListViewコントロール
ここは、foreachループで使用していたコードブロックです。どのようにParallel.Foreachを動かすのですか?
//Set cursor to WaitCursor Style
this.Cursor = Cursors.WaitCursor;
//Clear Messages RichTexBox
this.rtbMessages.Clear();
//Loop through each selected file
foreach (ListViewItem Item in lvMessageFiles.Items)
{
//Check if item is selected in the listview
if (Item.Selected && rtbMessages.TextLength < rtbMessages.MaxLength)
{
//Get Path to message file
filename = String.Format("{0}\\Data\\Log\\{1}.log", Global.AppPath, Item.SubItems[0].Text);
//Set Timeline Events calendar to selected items created date
cvTimeline.ShowDate(Convert.ToDateTime(lvMessageFiles.SelectedItems[0].SubItems[2].Text));
//Check if file exists
if (File.Exists(filename))
{
//streamreader to read the file
reader = new StreamReader(filename);
//to copy the read content in to richtextbox
string MessageContents = String.Format("{0}\n{1}\n", ("file:///" + filename.Replace(" ", "%20").Replace("\\", "/")), reader.ReadToEnd());
rtbMessages.Text += MessageContents;
// closing streamreader
reader.Close();
}
}
}
//Set cursor to WaitCursor Style
this.Cursor = Cursors.Default;
良い点...と、すべてのこれらのファイルは、同じドライブにでもフォルダになり、それを並列化するためにあまり意味がありません。各ファイルのないCPU集中処理が存在しないからです。 –
両方とも良い点ですが、Parallel.ForEachでは順序が保証されていないので、すべてのログファイルはいくらかランダムな順序になります – BrokenGlass