Cancel a pending async operation?

  • Thread starter Thread starter Anders R
  • Start date Start date
A

Anders R

Hi,

Anyone know howto cancel a pending async operation?

I'm using the .NET 2.0 SerialPort and I start a BeginRead(...);
What I like to do is to cancel that pending read. In c++ or with api calls
you could use PurgeComm(handle, PURGE_RXABORT);

I can't use PurgeComm in my case, any suggestions?

Best Regards

Anders
 
Anders R said:
Hi,

Anyone know howto cancel a pending async operation?

I'm using the .NET 2.0 SerialPort and I start a BeginRead(...);
What I like to do is to cancel that pending read. In c++ or with api calls
you could use PurgeComm(handle, PURGE_RXABORT);

You start a begin read on what? The BaseStream?

Chances are there isn't an easy method, can you not use ReadExisting and the
DataRecieved event to get around needing async?
 
Hi Daniel,

Yes, I use the SerialPort.BaseStream.BeginRead() operation.
No, I can't use the ReadExisting operation and DataRecived event...

If there isn't any easy method, is there any other way to doit?

Regard
Anders
 
Anders R said:
Hi Daniel,

Yes, I use the SerialPort.BaseStream.BeginRead() operation.
No, I can't use the ReadExisting operation and DataRecived event...

If there isn't any easy method, is there any other way to doit?

Digging around in the internals and see if the library they use offers a way
to cancel outstanding requests, and then pinvoking . I don't know what the
serial port class is built upon, alas. There is nothing I'm aware of at the
top level of the framework that allows canceling async operations.

However, have you tried using PurgeComm? Or perhaps just using pinvoke
instead of the serial port class? You might also be able to write your own
semi-async read code using a seperate thread and polling the BytesToRead
field.
 
Hi Daniel,

I have just solved my problems :)
If I use the SerialPort.DiscardOutBuffer() operation, then the Read Callback
is called and then I can call SerialPort.EndRead(asyncResult);

So, the solution for me was to use "DiscardOutBuffer()".

Btw, It's not possible for me to use the PurgeComm() because I don't know
the Handle to the SerialPort that I'm using,
I haven't found any way to get the handle for the SerialPort. If someone
knows howto get the handle that the SerialPort is using, please let me know.
Allways good to know for the future :)

Thanks for your time & help!

Best Regards

Anders R
 

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

Back
Top