Re: Threads and Exceptions



This is what i noticed in a .net 1.1 app.

you have to try catch within the function that the thread executes. if
you dont have a try catch in the function then you dont get notified
about the unhandled exception.
Dont know if things have changed since then.

DaveC wrote:
I am trying to use a ssh stream reader that has no timeout method. It
simply watches the stream until an escape character appears in the
stream. It then returns the contents of the buffer. Thus, when the
server never responds to a read action (no escape character appears in
the stream), the program appears hung while the thread is waiting for
a stream character that never comes.

To solve this, I issue the stream read inside of a 'try' block with a
catch for a custom exception. Just before the call to the stream
reader, I launch a watchdog function with its own thread. The watchdog
sleeps for 3 seconds and then checks the buffer (passed in by
reference) to see if it is still empty. If it is empty, it throws the
customer exception and pulls the main thread back to the catch portion
of the try.

This sounds good, but isn't working. When the exception throws in the
watchdog thread, it is treated like an unhandled exception even tho
the thread was launched from inside of a 'try' that has an explicit
handler for that exception.

In the code snippet below, as part of my efforts to figure this out, I
have the watchdog throw an exception for either condition of the read
stream. If the stream has something in it, one exception is thrown,
and if the stream buffer is still empty another exception is thrown.
Again, both are treated as unhandled even tho the function was launced
with its own thread from inside the 'try' block.

How can I get these exceptions to be handled by the 'try' block catch
blocks?

Dave

snippet:

try
{
Thread thd_watchdog = new Thread(delegate()
{ Read_Watchdog(ref response, 200); });
thd_watchdog.Start();
response = ssh.ReadResponse();
}
catch (ExpireException)
{ return "Error opening SSH stream to " + proxyServer; }
catch (ClearException)
{ return "Success opening SSH stream to " +
proxyServer; }


private ParameterizedThreadStart Read_Watchdog(ref string response,
int i_timer)
{
Thread.Sleep(i_timer);
if (response.Length > 0)
throw new ClearException();
else
throw new ExpireException();
}
.



Relevant Pages

  • Re: Assembly conversion to Pocket Pc format
    ... This exception could be for many reasons. ... HttpWebResponse resp = req.GetResponse; ... Stream respStream = resp.GetResponseStream; ... upload and download files in compact framework. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Assembly conversion to Pocket Pc format
    ... except the error message "An error message ... I could download all other type of files as given in the below ... detailed information on the exception, but from the error it looks like CF ... Stream respStream = resp.GetResponseStream; ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: HttpWebRequest.GetRequestStream times out
    ... When I encounter this exception, this is printed in the log: ... Stream sendStream = httpReq.GetRequestStream; ... you never read more than 1 kB of the response, ... The request timeout is set to 100 seconds, and the requests that time out ...
    (microsoft.public.dotnet.framework.performance)
  • Security problems with new Service Pack
    ... I'm using dotnet 1.0 and I developped a whole remoting system which was ... It seems that the Service pack adds some security restrictions, ... I got an exception when trying to invoke a remote method on a remote object: ... (Stream serializationStream, HeaderHandler handler, Boolean fCheck, ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: XLANGPart.LoadFrom(stream) failing - UnauthorizedAccessException
    ... A MemoryStream will always return this exception ... My component then generates a PDF stream using Crystal Reports, ... ReportDocument report = new ReportDocument; ...
    (microsoft.public.biztalk.general)

Quantcast