Device.Net: Cancellation Tokens and Timeouts
Hi. May be will be good if you add CancellationTokenSource or just timeout for read\write operation. Something like this:
//One sec timeout by default
public int Timeout { get; set; } = 1000;
…
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(Timeout);
await _ReadFileStream.ReadAsync(bytes, 0, bytes.Length, cancellationTokenSource.Token);
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 27 (17 by maintainers)
Commits related to this issue
- Add optional cancellation token parameter #2 — committed to MelbourneDeveloper/Device.Net by deleted user 4 years ago
- Implement the parameter in more places #2 — committed to MelbourneDeveloper/Device.Net by deleted user 4 years ago
- Implement SynchronizeWithCancellationToken on on Android #2 — committed to MelbourneDeveloper/Device.Net by www 4 years ago
- Merge branch '#2-CancellationTokens' into develop — committed to MelbourneDeveloper/Device.Net by www 4 years ago
@datoml check out this branch
https://github.com/MelbourneDeveloper/Device.Net/tree/%232-CancellationTokens?files=1
Getting closer
@datoml @skykharkov @fengqiangboy @fxjoos @mxjones
The library now supports cancellation tokens in the develop branch. It is mostly untested so I expect there to be issues. I will test when after work hours at some point soon.
Great to hear, that you implement it with an optional cancellation token. Is there a plan when it is open to try it out? 😃
@skykharkov @fxjoos @fengqiangboy
I’ve decided that I will add an optional cancellation token to the read/write methods. It makes a lot of sense. This way, you can handle timeouts without worrying about setting it at the constructor level and you can cancel whenever you like.
The only thing is that the onus will be on you to make sure that the device handle etc. is cleaned up afterwards.
In
WindowsSerialPortDevice
,I add a timeout parameter toInitialize
function. It look like thisWindowsSerialPortDevice.Initialize
:In
WindowsHidDevice
, I add a timeout parameter toReadReportAsync
method like :I have implement SerialPort Device timeout handle and HID device timeout handle in my project, but I have no idea to implement this in other platform, I have not use in other pltform.
@fxjoos thanks for the detailed information. I’ll use this as I try to fix the issue.
@fengqiangboy I will try to do this.
Yes, I agree with you. If timeouts are available then cancellation tokens are probably not needed.
@MelbourneDeveloper , yes you are right if timeouts are available then cancellation tokens are probably not needed.
To test the API with my reader (a Motorola/Symbol DS9208 barcode reader), I created a WPF test app with 2 buttons, one to start listening and one to stop.
Here is the code for timeout in WindowsHidApiService class:
A System.InvalidOperationException is thrown as soon as the code reach this constructor with the message:
I also tried with a CancellationToken instead. Test app code is as following:
Where “_device” is an IDevice (hid).
While the code is “awaiting” the WriteAndReadAsync, I call _cancellationTokenSource.Cancel() from another method triggered by a second button:
The WriteReadAsync method in DeviceBase:
Then in WindowsHIDDevice class for read method:
When the Cancel() method is called on the CancellationTokenSource nothing happens, no logs, no trace.
Did I miss something is this code ?
@MelbourneDeveloper It is possible to cancel a read task now? Because our hid device sometime can’t response when it sleep time. And if I call
Device.WriteAndReadAsync
when device is sleep, It will waiting forever, and can’t send next data in other thread.@fengqiangboy ok.
@fengqiangboy what case? If there is a problem, how do you know to cancel the task? Are you suggesting a timeout?
Some time the device not wrok correct, and not response. So we should cancel the read task, and tell user some error happen.