Link to home
Start Free TrialLog in
Avatar of somnatic
somnatic

asked on

WriteFile and CloseHandle seem to hang until further input

So ... let's start :)

I have some USB device which reports as HID compliant Device. I can turn and push this device.

My program is written in C#, but it looks like the program lies with some of the API functions.

I can push and rotate this device and via ReadFile I can get the data (what has been done / rotated / pushed).

BUT:
I should also be able to write to that device (therefore 1 (maybe 2) bytes are sent to the device).
The Problem:
When I call WriteFile with the handle at first my whole program seems to hang (I watched it in the debugger and it looks like WriteFile doesn't do anything). When I now rotate (or push)  the device (so that some data should be sent from the device to my PC) the data reaches me and WriteFiles continues (and ends with success).

The same applies for CloseHandle

I call CloseHandle -> Everything hangs -> I move the device -> CloseHandle continues and succeeds (no error)


I do the reading in a separate Thread - so could this be the problem?

Can anyone out there maybe help me ?
 
Avatar of lemmeC
lemmeC

The thread for reading seems to be having a higher priority. Try using the same priority for the ReadFile and WriteFile threads.
Or the WriteFile Thread could be waiting for some data set in the ReadFile Thread.
Avatar of somnatic

ASKER

hmm .. how can i set the priority in C# ?
ok . .found it out by myself ..

but that didn't help ...
I tried BelowNormal and Lowest, but nothing changed ..


Till now I think that:
* The thread which reads, has already called ReadFile(...)
* The thread which writes, calls WriteFile()
Now WriteFile has to wait until ReadFile has finished ...
then WriteFile is executed


any ideas ?
If the reading thread is reading constantly, WriteFile might be blocking until the last ReadFile issued is completed, yes. I suggest you use the asynchronous I/O read function, ReadFileEx() to do your reading. While waiting for ReadFileEx's completion, the reading thread can check to see if the writer thread is issuing a WriteFile function call, and if it is, cancel its read attempt via CancelIO, to allow the write to go through.
@ aib_42

that sounds like a possible solution .. do you have any code for this ?

i tried to cancel the ReadFile with CancelIo before, but nothing happened ...
ok .. another one ...

I tried to build that OVERLAPPED structure in C# ...

Appearently I did at least something right:

            [StructLayout(LayoutKind.Sequential)]
                  private unsafe struct OVERLAPPED
            {
                  public int Internal;
                  public int InternalHigh;
                  public int Offset;
                  public int OffsetHigh;
                  public int Handle;
            }


but when I issue this structure (overlap = new OVERLAPPED) to
ReadFile(HidHandle, BufBytes, InputReportByteLength, ref BytesRead, ref overlap)

it says it can't convert from ....OVERLAPPED to int*

shouldn't ref give out the pointer ?
forget that last one ...

caps suck ;)
You're on the right track.  Use overlapped I/O, or pass your write data to a thread that does the actual write.
hmm .. what do you mean by the second part ?

why would I use another thread if I can't stop ReadFile to wait for input ?
You may want to try switching to ReadFileEx() since "it is designed solely for asynchronous operation", quoting from the SDK reference :).

I have some synchronization ideas in mind, but I'm too sleepy to get my head together right now. If you still need advice by tomorrow (by when I will be at work ;) I can hopefully spit out some suggestions. Unfortunately, I have no source code at hand. (But I can give you a nice list of hacks and workarounds I had to try for a project, if you ever want to hate Win32 IPC.)

By the way, CancelIO has to be called from the thread which called the R/W operation (which also means that there is no way to block a synchronous operation). You would need the reading thread to check if there are any write requests and cancel its own read if so.
Typos, grammar mistakes, sleepiness, 9:25am. Please excuse the orc.
oh I excuse everything as long as it helps ;)

I tries ReadFileEx but couldn't get the thing with that Overlapped Structure right (too dumb probably).

concerning CancelIO ... did you have to say that ... ? that was exactely what I didn't want to hear ;)
ahem .. and .. are u saying that there is no way at all to "cancel" that ReadFile operation ?

(i don't care if it's a beautiful way or not ;) )
SOLUTION
Avatar of aib_42
aib_42

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ooook .. now this looks like it could work ..

Thanks to all who answered in this Thread
I'll split the points to the two people who helped me most ...

Thank you all ...