Sunil
Sunil

Reputation: 2905

Handle thread abort exception in threads....?

Child thread is waiting for response from webserver, and is having a timeout value too.
If timeout value comes first before getting response from web server, it will proceed to request timeout and thread abort exceptions.
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.

Upvotes: 0

Views: 1255

Answers (1)

TheMerovingian
TheMerovingian

Reputation: 648

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. So surrounding the line where you make the GetResponse() call with a try-catch statement should allow you to log information to a file.

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().

Upvotes: 1

Related Questions