Matchcollection Parallel.Foreach

时间:2012-05-28 18:27:39

标签: c# multithreading parallel-processing

我正在尝试为matchcollection创建一个Parallel.Foreach循环。 这是我建造的刮刀。 我只需要知道在Parallel.Foreach中放入什么

MatchCollection m = Regex.Matches(htmlcon, matchlink, RegexOptions.Singleline);

                Parallel.ForEach(WHAT DO I PUT HERE? =>
                {

                        Get(match.Groups[1].Value, false);
                        Match fname = Regex.Match(htmlcon, @"<span class=""given-name"(.*?)</span>", RegexOptions.Singleline);
                        Match lname = Regex.Match(htmlcon, @"span class=""family-name"">(.*?)</span>", RegexOptions.Singleline);

                        firstname = fname.Groups[1].Value;
                        lastname = lname.Groups[1].Value;

                        sw.WriteLine(firstname + "," + lastname);
                        sw.Flush();

                }):

我试过了:

Parallel.ForEach<MatchCollection>(m,match  =>

但没有运气!

提前致谢! :)

1 个答案:

答案 0 :(得分:14)

这是因为Parallel.ForEach期望通用IEnumerableMatchCollection只实现非通用的。{/ p>

试试这个:

Parallel.ForEach(
    m.OfType<Match>(),
    (match) =>
    {
    );