469,641 Members | 1,134 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,641 developers. It's quick & easy.

Can't get Timer to work with StreamReader.

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

Nov 16 '05 #1
3 1972
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
redneon wrote:
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

Nov 16 '05 #2
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

"Joakim Karlsson" wrote:
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
redneon wrote:
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

Nov 16 '05 #3
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.<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Jim Hammond | last post: by
6 posts views Thread by Antti Laakso | last post: by
12 posts views Thread by Crirus | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.