简体   繁体   English

处理线程中的线程中止异常…?

[英]Handle thread abort exception in threads…?

Child thread is waiting for response from webserver, and is having a timeout value too. 子线程正在等待来自Web服务器的响应,并且也具有超时值。
If timeout value comes first before getting response from web server, it will proceed to request timeout and thread abort exceptions. 如果超时值在从Web服务器获得响应之前首先出现,它将继续请求超时和线程异常中止。
How do we handle this and log into a text file….? 我们该如何处理并登录到文本文件中……?
Parent Thread 父线程

  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
 }

Child Threads 子线程

private void CallBackFunc(object obj)
{
     UrlData msgObj = (UrlData)obj;
   WebRequest lWebRequest = WebRequest.Create(psUrl);
   lWebRequest.Timeout = 60000;
   WebResponse lWebResponse = lWebRequest.GetResponse;

    msgObj.FinishEvent.Set();
}

Object for communication across threads 跨线程通信的对象

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;
  }
}


Thanks. 谢谢。

From what I understand, you're asking how to log to a file if your child thread times out? 据我了解,您在问如果子线程超时该如何登录文件?

If so, I believe that the GetResponse() method throws WebException if the time out limit is reached before it receives a response. 如果是这样,我相信如果在收到响应之前已达到超时限制,则GetResponse()方法将引发WebException So surrounding the line where you make the GetResponse() call with a try-catch statement should allow you to log information to a file. 因此,使用try-catch语句包围GetResponse()调用的行应该使您可以将信息记录到文件中。

Example: 例:

try {
    WebResponse lWebResponse = lWebRequest.GetResponse;
} catch (WebException ex) {
    // Write to file code in here
}

As GetResponse() is abstract the WebException is actually thrown by either the FileWebRequest or HttpWebRequest implementations of GetResponse() . 由于GetResponse()是抽象的,因此WebException实际上是由GetResponse()FileWebRequestHttpWebRequest实现抛出的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM