[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> </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> </DIV>
<DIV> //Code executed by the main thread:</DIV>
<DIV> protected byte[] Query(byte[]=20
sendData)<BR> {<BR> //Set all=20
values<BR> data =3D sendData;</DIV>
<DIV> </DIV>
<DIV> queryThread =3D new Thread(new=20
ThreadStart(ExecuteQuery));<BR> =
=20
<BR> //Start the=20
query<BR> queryThread.Start();<BR> <BR>=
//Wait=20
for the query to exit<BR> if (!queryThread.Join(timeout =
*=20
1000))<BR> {<BR> queryThread.Abor=
t();<BR> throw=20
new Exceptions.QueryTimeoutException("Query Timeout: " + =
timeout.ToString() + "=20
seconds");<BR> }<BR> <BR> &n=
bsp;//Return=20
the response, which is now saved in the local=20
variable<BR> return =
queryResponse;<BR> }</DIV>
<DIV> </DIV>
<DIV>// Code executed by the queryThread</DIV>
<DIV> private void=20
ExecuteQuery()<BR> {<BR> UdpClient client =
=3D new=20
UdpClient();<BR> //Connect to the server and send the =
query=20
data<BR> try<BR> {<BR>  =
; client.Connect(ip,port);<BR> client.Send(da=
ta,data.Length);<BR> }<BR> catch=20
(Exception=20
e)<BR> {<BR> client.Close();<BR>&=
nbsp; throw=20
new Exceptions.InvalidHostException("Unknown host: " +=20
ip,e);<BR> }</DIV>
<DIV> </DIV>
<DIV> //Listen for a response - This is the client=20
side<BR> IPEndPoint serverIPEndPoint =3D new=20
IPEndPoint(IPAddress.Any,0);<BR> <BR> /=
/Receive=20
the=20
response<BR> try<BR> {<BR> &=
nbsp; queryResponse=20
=3D client.Receive(ref=20
serverIPEndPoint);<BR> }<BR> catch =
(Exception=20
e)<BR> {<BR> throw new=20
Exceptions.ConnectionRefusedException("The connection was refused by the =
remote=20
host: " + ip + ":" +=20
port.ToString(),e);<BR> }<BR> finally<B=
R> {<BR> client.Close();<BR> =
; }<BR> }</DIV>
<DIV> </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> </DIV>
<DIV>I also have a second question: </DIV>
<DIV> queryThread.Abort();<BR> &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> </DIV>
<DIV> </DIV>
<DIV>Thnx.</DIV>
<DIV>TP.</DIV></FONT></DIV></BODY></HTML>
------=_NextPart_000_000A_01C2EEF0.3D8D7E40--