子线程正在等待来自网络服务器的响应,并且也具有超时值。
如果超时值在从 Web 服务器获得响应之前先出现,它将继续请求超时和线程中止异常。
我们如何处理这个并登录到一个文本文件......?
父线程
public void Save()
{
List<Job> Jobs = PickJobs();
int workerThreads = 0,compThreads = 0;
ThreadPool.GetMinThreads(workerThreads, compThreads);
int requiredThreads = 15;
ThreadPool.SetMaxThreads(requiredThreads, compThreads);
WaitCallback waitCallBack = default(WaitCallback);
ManualResetEvent mEvent = default(ManualResetEvent);
foreach (Job _job in Jobs)
{
waitCallBack = new WaitCallback(CallBackFunc);
mEvent = new ManualResetEvent(false);
events.Add(mEvent);
ThreadPool.QueueUserWorkItem(waitCallBack, new UrlData(_job, mEvent, HttpContext.Current));
}
WaitHandle.WaitAll(events.ToArray(), 300000);//05 Minutes
}
子线程
private void CallBackFunc(object obj)
{
UrlData msgObj = (UrlData)obj;
WebRequest lWebRequest = WebRequest.Create(psUrl);
lWebRequest.Timeout = 60000;
WebResponse lWebResponse = lWebRequest.GetResponse;
msgObj.FinishEvent.Set();
}
用于跨线程通信的对象
public class UrlData
{
public Job job;
public ManualResetEvent FinishEvent;
public HttpContext HttpContextRef;
public UrlData(Job pJob, ManualResetEvent pEvent, HttpContext pContext)
{
job= pJob;
FinishEvent = pEvent;
HttpContextRef = pContext;
}
}
谢谢。