|
楼主 |
发表于 2013-4-15 21:31:42
|
显示全部楼层
问题解决了,主要是定义了一个静态的Starti的变量,判断如果为0的话就证明是第一条线程,然后就等待第一条线程运行一次“while (ConTask.RunType)”里的代码才开始让其他线程进入
[code=csharp] /// <summary>
/// 爬虫运行类
/// </summary>
class SpiderRun
{
/// <summary>
/// 判断是否为第条线程
/// </summary>
private static int Starti = 0;
private void Run()
{
while (Starti == 1)
{
Thread.Sleep(2000);
}
if (Starti == 0)
{
Starti = 1;
}
HttpHelper Http = new HttpHelper();
HttpItem Hitem = new HttpItem()
{
URL = "",
ResultType = ResultType.String
};
while (ConTask.RunType)
{
UrlClass task = ConTask.GetTask();
if (task == null) continue;
Hitem.URL = task.Url;
ConTask.WriteLine(Thread.CurrentThread.Name.ToString()+ "号蜘蛛正在爬行:" + task.Url + "{换行}");
string HtmlStr = Http.GetHtml(Hitem).Html;
Hashtable urlList = ConTask.GetUrlArrList(task.Url, HtmlStr);
foreach (DictionaryEntry url in urlList)
{
if (ConTask.OldList.ContainsValue(url.Value)) continue;
Regex reg = new Regex(@"^http:\/\/([\w-]+(\.[\w-]+)+(\/[\w-.\/\?%&=\u4e00-\u9fa5]*)?)?$");
bool m = reg.IsMatch(url.Value.ToString());
if (!m) continue;
if (url.Value.ToString().Replace("http://", "").Replace("HTTP://", "").Trim().Length < 5) continue;
ConTask.OldList.Add(new RandClass(35).randStr,url.Value);
ConTask.taskList.Add(new UrlClass(url.Key.ToString(), url.Value.ToString()));
}
if (Starti == 1) Starti = 2;
}
ConTask.RunThreadCount--;
if (ConTask.RunThreadCount < 1)
{
if (ConTask.IsStopType)
{
ConTask.WriteLine("[任务已经于 " + DateTime.Now.ToString() + " 终止运行!]{换行}");
ConTask.Threads = null;
}
else
{
ConTask.WriteLine("[任务运行完毕!]{换行}");
}
}
if (ConTask.softMainFrm != null) ConTask.softMainFrm.TaskFinish();
}
public void Start()
{
ConTask.RunType = true;
SpiderRun.Starti = 0;
//if (ConTask.ThreadCount > ConTask.taskList.Count) ConTask.ThreadCount = 1;
ConTask.Threads = new System.Threading.Thread[ConTask.ThreadCount];
ConTask.RunThreadCount = 0;
for (int i = 0; i < ConTask.ThreadCount; i++)
{
ConTask.Threads = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
ConTask.Threads.Name = i.ToString();
ConTask.Threads.IsBackground = true;
ConTask.RunThreadCount++;
ConTask.Threads.Start();
}
}
}[/code] |
|