0
子線程正在等待來自web服務器的響應,並且也具有超時值。
如果在獲取來自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;
}
}
感謝。