我正在尝试使用多个线程发出网络请求,但如果我尝试使用超过 2 个线程,则会出现错误
Index was outside the bonds of the array
在这一行:
string username = ScrapeBox1.Lines[NamesCounter].ToString();
代码如下:
while (working)
{
while (usernamescount > NamesCounter)
{
string username = ScrapeBox1.Lines[NamesCounter].ToString();
string url = "http://www.someforum.com/members/" + username + ".html";
var request = (HttpWebRequest)(WebRequest.Create(url));
var response = request.GetResponse();
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20100101 Firefox/16.0";
using (var responseStream = response.GetResponseStream())
{
using (var responseStreamReader = new StreamReader(responseStream))
{
var serverResponse = responseStreamReader.ReadToEnd();
int startpoint = serverResponse.IndexOf("Contact Info</span>");
try
{
string strippedResponse = serverResponse.Remove(0, startpoint);
ExtractEmails(strippedResponse);
}
catch { }
}
}
NamesCounter++;
textBox1.Text = NamesCounter.ToString();
}
}
最佳答案
此代码不是线程安全的。
您需要执行 HttpWebRequest 的代码是原子的,并且位于循环遍历集合的上下文之外。
例如
public void MakeHttpWebRequest(string userName)
{
string url = "http://www.someforum.com/members/" + userName + ".html";
var request = (HttpWebRequest)(WebRequest.Create(url));
var response = request.GetResponse();
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20100101 Firefox/16.0";
using (var responseStream = response.GetResponseStream())
{
using (var responseStreamReader = new StreamReader(responseStream))
{
var serverResponse = responseStreamReader.ReadToEnd();
int startpoint = serverResponse.IndexOf("Contact Info</span>");
try
{
string strippedResponse = serverResponse.Remove(0, startpoint);
ExtractEmails(strippedResponse);
}
catch { }
}
}
}
假设 ScrapeBox.Lines 实现 IEnumerable,我建议使用 Parallel.ForEach并将 ScrapeBox.Lines 作为要迭代的 IEnumerable 传递。
现在,还有一个额外的问题,从 HttpWebRequest 读取响应的代码仍然需要将其输出写入共享位置。以线程安全的方式实现这一点。执行此操作的常见方法是使用信号量。您需要一个可供每个线程实例访问的对象。类级私有(private)变量private object sharedMutex = new object();
会工作。然后代码ExtractEmails(strippedResponse);
应该改为
lock(sharedMutex)
{
ExtractEmails(strippedResponse);
}
没有 ExtractEmails(<string>)
的代码方法,我无法为此提供线程安全的实现,因此该解决方案的一部分仍然可能会导致问题。
关于多线程 Web 请求上的 C# 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13224471/