我有一个网站,我编写了一个 HttpModule 来转换所有链接,所以一切都很好,直到我要在转换 URL 中使用并行性。
这是我的测试控制台应用程序:
class Program
{
static void Main(string[] args)
{
new Job().Do();
}
}
public class Job
{
public void Do()
{
string content = @"
new link1 href=""www.yahoo1.com"" end
new link2 href=""www.yahoo2.com"" end
new link3 href=""www.yahoo3.com"" end
new link4 href=""www.yahoo4.com"" end
new link5 href=""www.yahoo5.com"" end
new link6 href=""www.yahoo6.com"" end
";
string newcontent = Transformlink(content);
Console.WriteLine(content);
Console.WriteLine();
Console.WriteLine(newcontent);
Console.ReadLine();
}
private string Transformlink(string content)
{
List<UrlIndex> AllUrls = GetUrls(content);
List<Task> TaskPool = new List<Task>();
foreach (UrlIndex Item in AllUrls)
TaskPool.Add(Task.Factory.StartNew(() => TransformUrl(Item)));
Task.WaitAll(TaskPool.ToArray());
return ReplaceUrlWithTransformUrl(content, AllUrls);
}
private string ReplaceUrlWithTransformUrl(string content, List<UrlIndex> AllUrls)
{
for (int i = AllUrls.Count - 1; i >= 0; i--)
{
UrlIndex CurrentItem = AllUrls[i];
content = content.Substring(0, CurrentItem.StartIndex) + CurrentItem.TransformedUrl + content.Substring(CurrentItem.EndIndex);
}
return content;
}
private void TransformUrl(UrlIndex urlindex)
{
urlindex.TransformedUrl = string.Format("Google{0}.com", new Random().Next(100, 999).ToString());
}
private List<UrlIndex> GetUrls(string content)
{
//Get Start And End Index, Get Url Set TransformedUrl = Url
List<UrlIndex> AllUrls = new List<UrlIndex>();
int startindex = 0;
int endIndex = 0;
int previousindex = 0;
while (startindex != -1)
{
startindex = content.IndexOf("href=\"", previousindex);
if (startindex == -1)
break;
startindex += 6;
previousindex = startindex;
endIndex = content.IndexOf("\"", previousindex);
if (endIndex == -1)
break;
previousindex = endIndex;
string url = content.Substring(startindex, endIndex - startindex);
AllUrls.Add(new UrlIndex() { StartIndex = startindex, EndIndex = endIndex, Url = url, TransformedUrl = url });
}
return AllUrls;
}
}
public class UrlIndex
{
public int StartIndex { get; set; }
public int EndIndex { get; set; }
public string Url { get; set; }
public string TransformedUrl { get; set; }
}
结果必须是:
new link1 href=""www.Google859.com"" end
new link2 href=""www.Google245.com"" end
new link3 href=""www.Google749.com"" end
new link4 href=""www.Google345.com"" end
new link5 href=""www.Google894.com"" end
new link6 href=""www.Google243.com"" end
这正是我想要的。
但结果是:
new link1 href=""www.yahoo1.com"" end
new link2 href=""www.yahoo2.com"" end
new link3 href=""www.yahoo3.com"" end
new link4 href=""www.yahoo4.com"" end
new link5 href=""www.yahoo5.com"" end
new link6 href=""www.Google125.com"" end
如您所见,仅转换了最后一个链接。在某些情况下:
new link1 href=""www.yahoo1.com"" end
new link2 href=""www.yahoo2.com"" end
new link3 href=""www.Google285.com"" end
new link4 href=""www.yahoo4.com"" end
new link5 href=""www.yahoo5.com"" end
new link6 href=""www.Google125.com"" end
控制台项目在 .NET 4 中
这是我的错吗?为什么所有任务都不起作用?线Task.WaitAll(TaskPool.ToArray());
不够?有什么建议吗?