D
DaveC
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();
}
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();
}