[Mono-list] Threading + Exception Handling (2 Questions)

Timothy Parez tpsoftware@users.sourceforge.net
Thu, 20 Mar 2003 14:51:51 +0100


This is a multi-part message in MIME format.

------=_NextPart_000_000A_01C2EEF0.3D8D7E40
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

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 =3D sendData;

   queryThread =3D new Thread(new ThreadStart(ExecuteQuery));
           =20
   //Start the query
   queryThread.Start();
  =20
   //Wait for the query to exit
   if (!queryThread.Join(timeout * 1000))
   {
    queryThread.Abort();
    throw new Exceptions.QueryTimeoutException("Query Timeout: " + =
timeout.ToString() + " seconds");
   }
  =20
   //Return the response, which is now saved in the local variable
   return queryResponse;
  }

// Code executed by the queryThread
 private void ExecuteQuery()
  {
   UdpClient client =3D 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 =3D new IPEndPoint(IPAddress.Any,0);
  =20
   //Receive the response
   try
   {
    queryResponse =3D 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:=20
    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.
------=_NextPart_000_000A_01C2EEF0.3D8D7E40
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1106" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>
<DIV>Hello,</DIV>
<DIV>&nbsp;</DIV>
<DIV>I have class which creates another thread</DIV>
<DIV>now that other tread can throw a custom error, but</DIV>
<DIV>when this error is thrown I want the main thread of the class =
library to=20
receive this exception so that the client application which uses my =
library=20
get's this error and all execution of other code in my class library is=20
stopped.</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;//Code executed by the main thread:</DIV>
<DIV>&nbsp;&nbsp;protected byte[] Query(byte[]=20
sendData)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;//Set all=20
values<BR>&nbsp;&nbsp;&nbsp;data =3D sendData;</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;&nbsp;&nbsp;queryThread =3D new Thread(new=20
ThreadStart(ExecuteQuery));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;=20
<BR>&nbsp;&nbsp;&nbsp;//Start the=20
query<BR>&nbsp;&nbsp;&nbsp;queryThread.Start();<BR>&nbsp;&nbsp;&nbsp;<BR>=
&nbsp;&nbsp;&nbsp;//Wait=20
for the query to exit<BR>&nbsp;&nbsp;&nbsp;if (!queryThread.Join(timeout =
*=20
1000))<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;queryThread.Abor=
t();<BR>&nbsp;&nbsp;&nbsp;&nbsp;throw=20
new Exceptions.QueryTimeoutException("Query Timeout: " + =
timeout.ToString() + "=20
seconds");<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&n=
bsp;//Return=20
the response, which is now saved in the local=20
variable<BR>&nbsp;&nbsp;&nbsp;return =
queryResponse;<BR>&nbsp;&nbsp;}</DIV>
<DIV>&nbsp;</DIV>
<DIV>// Code executed by the queryThread</DIV>
<DIV>&nbsp;private void=20
ExecuteQuery()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;UdpClient client =
=3D new=20
UdpClient();<BR>&nbsp;&nbsp;&nbsp;//Connect to the server and send the =
query=20
data<BR>&nbsp;&nbsp;&nbsp;try<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp=
;&nbsp;client.Connect(ip,port);<BR>&nbsp;&nbsp;&nbsp;&nbsp;client.Send(da=
ta,data.Length);<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;catch=20
(Exception=20
e)<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;client.Close();<BR>&=
nbsp;&nbsp;&nbsp;&nbsp;throw=20
new Exceptions.InvalidHostException("Unknown host: " +=20
ip,e);<BR>&nbsp;&nbsp;&nbsp;}</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;&nbsp;&nbsp;//Listen for a response - This is the client=20
side<BR>&nbsp;&nbsp;&nbsp;IPEndPoint serverIPEndPoint =3D new=20
IPEndPoint(IPAddress.Any,0);<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;/=
/Receive=20
the=20
response<BR>&nbsp;&nbsp;&nbsp;try<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&=
nbsp;&nbsp;queryResponse=20
=3D client.Receive(ref=20
serverIPEndPoint);<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;catch =
(Exception=20
e)<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;throw new=20
Exceptions.ConnectionRefusedException("The connection was refused by the =
remote=20
host: " + ip + ":" +=20
port.ToString(),e);<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;finally<B=
R>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;client.Close();<BR>&nbsp=
;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;}</DIV>
<DIV>&nbsp;</DIV>
<DIV>As you can see the ExecuteQuery() function can throw the=20
Exceptions.ConnectionRefusedException,</DIV>
<DIV>but my main thread never receives this error, so the execution of =
the code=20
in the main thread does not stop.</DIV>
<DIV>How can I fix this.</DIV>
<DIV>&nbsp;</DIV>
<DIV>I also have a second question: </DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;queryThread.Abort();<BR>&nbsp;&nbsp;&nbsp;&n=
bsp;throw=20
new Exceptions.QueryTimeoutException("Query Timeout: " + =
timeout.ToString() + "=20
seconds");</DIV>
<DIV>I abort the second thread, will the QueryTimeoutException still be =
thrown ?=20
(I hope so), or will the .Abort() cause an error</DIV>
<DIV>which will prevent from the QueryTimeoutException ever happening, =
in that=20
case how do I abort the second query and still throw the =
QueryTimeoutException=20
?</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>Thnx.</DIV>
<DIV>TP.</DIV></FONT></DIV></BODY></HTML>

------=_NextPart_000_000A_01C2EEF0.3D8D7E40--