Can't get Timer to work with StreamReader.

G

Guest

I have a program which is constantly reading from a stream and what I'm
wanting to do is, if the stream hasn't sent anything after a certain amount
of time then do something. I've tried doing this like this...

while(!done)
{
idleTimer.Start();
response = streamReader.ReadLine();
idleTimer.Stop();
....

The interval on the Timer is set to 10000 (10 seconds) but I've hung
response = streamReader.ReadLine() for five minutes and it still doesn't run
the Timer's Tick event. The interesting thing though, is if I do this...

idleTimer.Start();
MessageBox.Show("Testing...");
response = streamReader.ReadLine();
idleTimer.Stop();

....and then don't press the button on the message box, after 10 seconds the
Tick event is fired. So why not after 10 seconds of waiting for a response
from the stream? I've even tried sticking the timer stuff in it's own thread
but I get the same problem.

Please help,

Darrell
 
J

Joakim Karlsson

My guess is that the StreamReader.ReadLine method hangs the current
thread, which prevents it from pumping messages. MessageBox.Show keeps
pumping messages while the message box is displayed, and that lets the
timer messages be handled.

You could perhaps put the StreamReader code in a worker thread, keep the
timer on the ui thread, and when the timer fires check if anything has
been read.

Regards,
Joakim
 
G

Guest

I've tried putting the timer stuff in a new thread but I still get the same
problem. The method with the reader in is a thread off the UI in itself
already.

Darrell
 
J

Jeff Louie

redneon... I just finished a sample project that launches I/O threads
and calls WaitForThreadExit(waitForThreadTime). Basically, each I/O
thread callsback when done. The worker thread sleeps in a loop checking
to see if the I/O thread has returned. If the waitForThreadTime is
exceeded the worker thread exits the loop.

http://www.geocities.com/jeff_louie/call_console.htm

Regards,
Jeff
I've tried putting the timer stuff in a new thread but I still get the
same problem.<
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top