Alternative to FileStream.Read with a timeout

S

Stu

Hi, I'm using a FileStream to read from a USB device. Everything is
working perfectly, except if I try to read from the device when there
is nothing in the buffer, the FileStream.Read function blocks
permanently. I tried setting the ReadTimeout but I get an error
saying that this stream does not support timeouts... Is there any way
around this? Why would the stream not support timeouts?
 
P

Peter Duniho

Stu said:
Hi, I'm using a FileStream to read from a USB device. Everything is
working perfectly, except if I try to read from the device when there
is nothing in the buffer, the FileStream.Read function blocks
permanently. I tried setting the ReadTimeout but I get an error
saying that this stream does not support timeouts... Is there any way
around this? Why would the stream not support timeouts?

I don't know specifically why it wouldn't, but it's not surprising to me
that it might not.

I'm not really clear on why you're using FileStream, as opposed to
something else (for example, SerialPort) to read from the USB device. I
presume it has something to do with the specific device you're using.
But, if the USB device can be exposed as a serial port, you might find
that a more appropriate abstraction, since the SerialPort class does
support timeouts.

Barring that, IMHO a much better way to deal with timeouts anyway is to
just implement the timeout yourself. For example, just start a timer
for the length of time you want to use for the timeout and if the timer
expires before you've actually read anything, abort the operation (e.g.
by closing the FileStream).

Implementing the timeout yourself gives you a lot more control -- for
example, if instead you want to prompt the user and ask if the program
should continue waiting to see if data will appear -- than being tied to
the i/o object you're using, and of course gives you greater flexibility
in working with a variety of i/o objects with the same technique.

Pete
 
S

Stu

I don't know specifically why it wouldn't, but it's not surprising to me
that it might not.

I'm not really clear on why you're using FileStream, as opposed to
something else (for example, SerialPort) to read from the USB device.  I
presume it has something to do with the specific device you're using.
But, if the USB device can be exposed as a serial port, you might find
that a more appropriate abstraction, since the SerialPort class does
support timeouts.

Barring that, IMHO a much better way to deal with timeouts anyway is to
just implement the timeout yourself.  For example, just start a timer
for the length of time you want to use for the timeout and if the timer
expires before you've actually read anything, abort the operation (e.g.
by closing the FileStream).

Implementing the timeout yourself gives you a lot more control -- for
example, if instead you want to prompt the user and ask if the program
should continue waiting to see if data will appear -- than being tied to
the i/o object you're using, and of course gives you greater flexibility
in working with a variety of i/o objects with the same technique.

Pete

Thanks Pete, I'll give one or both of those solutions a try.
Basically the reason I'm using FileStream is because I've never done
USB communications before and the tutorials I could find regarding USB
communications in C# used FileStream. It's working perfectly well
other than this one issue...
 

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