On 2007-11-13 12:11:18 -0800, FP <fp@nospam.comsaid:
Hi,
after some deeper investigations i found out that received data are treated
in a separate thread,so i am beginning to wonder if i might really need to
create a new thread.I will better explain my application,now.
I think that for sure, you do not need to create a new thread. But
that's not to say that you can also just spend a long time reading data
from the SerialPort.
[...]
What suggested me to create a new thread is the fact that step8 can last
some minutes,but now,knowing that DataReceived is on another thread is
making me wonder....
Will the main app hang while i'm receiving the string from the remote
modem?
Not immediately, no. But depending on what thread pool is actually
being used for raising the DataReceived event, you could cause some
problems if you tie up a thread from the pool for a lengthy period of
time.
In most cases, the likelihood of a problem is low, but it does exist.
Obviously i will have to test (but i can't right now),but the fact that
DataReceived is already running on a separate thread is kinda confusing
me....
IMHO, you have a couple of options:
* Use the DataReceived event
* Use the SerialPort.BaseStream and use the async methods of that
stream (this is basically what Chris Mullins is suggesting)
I would ordinarily agree with Chris and suggest using the BaseStream.
This would involve calling Stream.BeginRead, and then in your callback
processing the data that's been read (retrieved by calling
Stream.EndRead) and calling Stream.BeginRead again.
The thing that gives me pause is that in the SerialPort docs, it says
that the base stream is not buffered, while the SerialPort class itself
is. I would be concerned that you could potentially lose data if you
aren't careful with how you use the BaseStream, by not having an
outstanding read posted when some data comes in.
Granted, it could be I'm just misunderstanding the implementation. I
don't really know much about SerialPort or of the Stream that it uses,
and it's possible that the docs are being misleading by saying that the
stream isn't buffered (they could be talking about some kind of buffer
other than at the lowest level, for example).
Basically, while I think you ought to be able to use the Stream
methods, the docs make me wonder about that, and it seems like the
DataReceived event should provide a similar enough behavior to be
sufficient.
So, how to use DataReceived? You should not spend any significant time
in your DataReceived handler. You should read whatever bytes are
available, appending them to some buffer somewhere. If and when that
buffer represents a complete transmission, then you do whatever
processing is appropriate for a completed transmission.
Basically, just let the SerialPort raise that event repeatedly until
you've received all of the data you need. Don't do any waiting in any
thread for data per se, other than the normal waiting that occurs in
any application (waiting for user input via the normal Windows event
mechanism, for example).
Pete