
使用線程池並發處理request請求及錯誤重試,使用委托處理UI界面輸出。
http://www.cnblogs.com/Charltsing/p/httpwebrequest.html
for (int j = 0; j < _userlist.Count; j++)
{
user = _userlist[j];
if (user.Length == 0) continue;
for (int k = 0; k < _passwordlist.Count; k++)
{
password = _passwordlist[k];
if (password.Length == 0) continue;
if (threadscount < _MaxThreadsCount)
{
if (_isStop)
{
WriteMessage("\r\n停止創建新線程,正在結束已運行的線程,請等待...\r\n");
goto StopHttp;
}
HttpWebRequestParameters param = new HttpWebRequestParameters(url, user, password,0,false);
ThreadPool.QueueUserWorkItem(new WaitCallback(MakeWebRequest), param);
Interlocked.Increment(ref threadscount);
Interlocked.Increment(ref requestcount);
}
else
{
k--;
}
WriteStatus("已使用線程數:" + threadscount.ToString() +
" 已發送請求數:" + requestcount.ToString() +
" 已完成請求數:" + responsecount.ToString());
}
System.GC.Collect();
}
***************************************
更新說明:
上面的方法使用線程池,這種方法沒有task方便。
我在2016年11月用async/await+httpclient 重新寫了一個版本,實現了完整的任務發布、異步提交http請求、超時或者手工取消的操作

此外,還寫了一個使用async+HttpWebRequest+task實現的異步提交任務及超時處理的代碼版本,這種方法不需要自己實現多線程,異步操作本身就是通過線程池來實現多線程的。


聯系QQ:564955427
歡迎交流!
