[C#] 纯文本查看 复制代码 /// <summary>
/// 工作主线程
/// </summary>
/// <param name="obj"></param>
private void ThreadWork()
{
int WorkerThreadsMax = 0, WorkerThreadsAv = 0;
//用户设置大于1000个线程是没有意义的。线程池最多支持1000个线程同时工作。
formsPram.WorkThreadNumber = formsPram.WorkThreadNumber > 1000 ? 1000 : formsPram.WorkThreadNumber;
ThreadPool.SetMinThreads(1, 1);
ThreadPool.SetMaxThreads(formsPram.WorkThreadNumber, formsPram.WorkThreadNumber);
//初始化扫描数据
InitiaData();
long speed = formsPram.NumberStart;
//解除http访问高并发限制,如果不设置该值.只能同时并发2 - 10个数据
ServicePointManager.DefaultConnectionLimit = 512;
while (!Stopeed)
{
ThreadPool.GetMaxThreads(out WorkerThreadsMax, out int CompletionPrortThreadsMax);
ThreadPool.GetAvailableThreads(out WorkerThreadsAv, out int CompletionPrortThreadsAv);
//设置显示空闲线程数
SetFreeThreadNumber(WorkerThreadsAv);
//显示正在工作的线程数
SetWorkerThreadNumber(WorkerThreadsMax - WorkerThreadsAv);
//设置显示一下找到的个数。
SetSuccessNum();
//设置已经扫描的个数
SetHandleNum();
if (WorkerThreadsAv > 0 && formsPram.NumberStart < formsPram.NumberEnd)
{
for (int i = 0; i < WorkerThreadsAv; i++)
{
if (TaskIndex >= weblist.Count)
{
break;
}
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPoolTask), weblist[TaskIndex++]);
}
Debug.WriteLine($"投递了{WorkerThreadsAv}个任务出去");
}
//检查一下是否已经抓取完毕了
if (TaskIndex >= weblist.Count)
{
AddLog("任务投递完成,等待子线程执行完毕后程序自动停止。");
break;
}
Thread.Sleep(1000);
}
while (true)
{
ThreadPool.GetMaxThreads(out WorkerThreadsMax, out int CompletionPrortThreadsMax);
ThreadPool.GetAvailableThreads(out WorkerThreadsAv, out int CompletionPrortThreadsAv);
if (WorkerThreadsMax != WorkerThreadsAv)
{
AddLog($"正在等待子线程结束,还剩余{WorkerThreadsMax - WorkerThreadsAv} 个任务正在执行");
}
else
{
break;
}
Thread.Sleep(1000);
}
BtnStart.Invoke(new Action(() =>
{
BtnStart.Enabled = true;
}));
AddLog("主线程停止了工作!");
}
/// <summary>
/// 任务线程池
/// </summary>
private void ThreadPoolTask(object obj)
{
DateTime dateTime = DateTime.Now;
var str = Work.HttpRequest(formsPram.WaitingTime, obj.ToString());
DateTime dateTime2 = DateTime.Now;
TimeSpan timeSpan = dateTime2 - dateTime;
if (timeSpan.TotalMilliseconds >= 4000)
{
AddLog($"线程超过4000毫秒才执行完成,总计耗时{timeSpan.TotalMilliseconds}毫秒");
}
// Debug.WriteLine($"耗时 {timeSpan.TotalMilliseconds} 毫秒 " + obj.ToString());
lock (objLock)
{
HandleNum++;
}
if (str.Contains(formsPram.Features1) && str.Contains(formsPram.Features2))
{
AddLog($"找到 {formsPram.InterfaseClass} ,域名是{obj}");
lock (objLock)
{
SuccessNum++;
}
CreateFiles($"类型:{formsPram.InterfaseClass} ---- 域名: {obj}");
return;
}
}
/// <summary>
/// GET方式 访问网页
/// </summary>
/// <param name="Timeout">访问超时限定</param>
/// <param name="Address">要访问的网址</param>
/// <returns></returns>
public static string HttpRequest(int Timeout, string Address)
{
if (string.IsNullOrEmpty(Address))
{
return "";
}
try
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Address);
webRequest.Method = "GET";
webRequest.Timeout = Timeout;
webRequest.ReadWriteTimeout = Timeout;
webRequest.KeepAlive = false;
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8);
var str = sr.ReadToEnd();
webRequest.Abort();
webResponse.Close();
sr.Close();
return str;
}
catch (Exception)
{
return "";
}
} |