Page 1 of 1

How To Cancel Async Read?

Posted: Thu Nov 05, 2009 7:54 pm
by AlexNek
Problem:
I start async read with something like it:
remoteService.Stream.BeginRead(readBuffer, 0, readBuffer.Length, new
AsyncCallback(ReadCallback), currentStream);

Then I wait when callback finished and read the data. Callback function look like:

private void ReadCallback(IAsyncResult result) { ...
readLength = stream.EndRead(result);
...
}
So the normal reading is running without troubles.


Now, my Bluetooth give me no answer so ReadCallback is not called. I
produce timeout event, send new data and try reading again. But now it is not possible because of franson exception "Call EndRead after
BeginRead".
When I call EndRead in timeout case then EndRead wait data endless.
So it is the question how can I cancel the current Async read and start the new one?

RE: How To Cancel Async Read?

Posted: Fri Nov 06, 2009 1:48 pm
by Jonas
You cannot cancel an async read but in reality you shouldn't have to either.
If you send data again, the "old" read is still valid for your "new" write (response).

Regards,
Jonas

RE: How To Cancel Async Read?

Posted: Mon Nov 16, 2009 12:18 pm
by AlexNek
When I'll try to send data again, I have the error message "Call EndRead after BeginRead" because EndRead wouldn't called because was answer timeout

RE: How To Cancel Async Read?

Posted: Mon Nov 16, 2009 4:29 pm
by Jonas
Answer timeout? What does that imply?

Regards,
Jonas

RE: How To Cancel Async Read?

Posted: Mon Nov 16, 2009 7:15 pm
by AlexNek
"Answer timeout?" - yes exact.
We sent data to device but device don't answer in 1 sec. Then we try it 2-times again. If always no answer then we make deciding about broken connection.
And it is exact our trouble. We couldn’t send anything after timeout.
As solution we switched to synchronous read + additional thread but we use a suggestion that stream.Length show us that any data was received.

RE: How To Cancel Async Read?

Posted: Mon Nov 16, 2009 7:31 pm
by Jonas
Ah, in that case you have to use synchronous read.

Regards,
Jonas

RE: How To Cancel Async Read?

Posted: Tue Nov 17, 2009 11:08 am
by AlexNek
But it means that I can't use asynchronous read in general because I don't know how to switch asyn/sync read dynamically.
I have some question about synchronous read but I think it would be better to open a new thread.