AlexNek
Posts: 11
Joined: Thu Nov 05, 2009 7:49 pm

How To Cancel Async Read?

Thu Nov 05, 2009 7:54 pm

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?

Jonas
Posts: 2319
Joined: Mon Jan 09, 2006 2:20 pm
Location: Sweden

RE: How To Cancel Async Read?

Fri Nov 06, 2009 1:48 pm

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

AlexNek
Posts: 11
Joined: Thu Nov 05, 2009 7:49 pm

RE: How To Cancel Async Read?

Mon Nov 16, 2009 12:18 pm

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

Jonas
Posts: 2319
Joined: Mon Jan 09, 2006 2:20 pm
Location: Sweden

RE: How To Cancel Async Read?

Mon Nov 16, 2009 4:29 pm

Answer timeout? What does that imply?

Regards,
Jonas

AlexNek
Posts: 11
Joined: Thu Nov 05, 2009 7:49 pm

RE: How To Cancel Async Read?

Mon Nov 16, 2009 7:15 pm

"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.

Jonas
Posts: 2319
Joined: Mon Jan 09, 2006 2:20 pm
Location: Sweden

RE: How To Cancel Async Read?

Mon Nov 16, 2009 7:31 pm

Ah, in that case you have to use synchronous read.

Regards,
Jonas

AlexNek
Posts: 11
Joined: Thu Nov 05, 2009 7:49 pm

RE: How To Cancel Async Read?

Tue Nov 17, 2009 11:08 am

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.

Return to “BlueTools”