1

Topic: HidSharp - Catch error when device is unplugged

I'm using the lib in windows.

I'm continuously watching the HidStream for data which means that at each callback from BeginRead() I start a new BeginRead().

That is the way that I found to continuously watch for new inputs.

The only problem is that when the device is disconnected from the USB port, there is an error that I can't handle.
This error is saved in Exception field of AsyncResult<T> but there are 2 problems there:

1. - The callback parameter is IAsyncResult, which doesn't have Exception field.
2. - The Exception field in AsyncResult<T> is not public, so I cannot access it anyway.

I managed to solve by making the field Exception Public and inside the callback handler I do a cast to AsyncResult<T> which I also made public.

Is that right? Is there any better way to do that?

Thanks in advance.