Hello,
 
I have class which creates another thread
now that other tread can throw a custom error, but
when this error is thrown I want the main thread of the class library to receive this exception so that the client application which uses my library get's this error and all execution of other code in my class library is stopped.
 
 //Code executed by the main thread:
  protected byte[] Query(byte[] sendData)
  {
   //Set all values
   data = "">
 
   queryThread = new Thread(new ThreadStart(ExecuteQuery));
           
   //Start the query
   queryThread.Start();
   
   //Wait for the query to exit
   if (!queryThread.Join(timeout * 1000))
   {
    queryThread.Abort();
    throw new Exceptions.QueryTimeoutException("Query Timeout: " + timeout.ToString() + " seconds");
   }
   
   //Return the response, which is now saved in the local variable
   return queryResponse;
  }
 
// Code executed by the queryThread
 private void ExecuteQuery()
  {
   UdpClient client = new UdpClient();
   //Connect to the server and send the query data
   try
   {
    client.Connect(ip,port);
    client.Send(data,data.Length);
   }
   catch (Exception e)
   {
    client.Close();
    throw new Exceptions.InvalidHostException("Unknown host: " + ip,e);
   }
 
   //Listen for a response - This is the client side
   IPEndPoint serverIPEndPoint = new IPEndPoint(IPAddress.Any,0);
   
   //Receive the response
   try
   {
    queryResponse = client.Receive(ref serverIPEndPoint);
   }
   catch (Exception e)
   {
    throw new Exceptions.ConnectionRefusedException("The connection was refused by the remote host: " + ip + ":" + port.ToString(),e);
   }
   finally
   {
    client.Close();
   }
  }
 
As you can see the ExecuteQuery() function can throw the Exceptions.ConnectionRefusedException,
but my main thread never receives this error, so the execution of the code in the main thread does not stop.
How can I fix this.
 
I also have a second question:
    queryThread.Abort();
    throw new Exceptions.QueryTimeoutException("Query Timeout: " + timeout.ToString() + " seconds");
I abort the second thread, will the QueryTimeoutException still be thrown ? (I hope so), or will the .Abort() cause an error
which will prevent from the QueryTimeoutException ever happening, in that case how do I abort the second query and still throw the QueryTimeoutException ?
 
 
Thnx.
TP.

Reply via email to