In my c++ windows application I’m using the CreateFile function to access a file. I’m using also the following functions to control my file:
DeviceIOControl
ReadFile
SetFilePointer
since I want that my application will access many positions in the file at the same time (from requests I get to my application) I want that my application will be async. I saw there is a flag for this called FILE_FLAG_OVERLAPPED but I don’t know how to use it and when do I know when it complete its operation.
is there any callback that I have to register?
if you can copy here a sample it will be helpful
thanks
You can just spawn a new thread and call ReadFie() synchronously from that second thread. You need to synchronize both threads using a synchronization object, such as an event or a critical section.
If you insist on the single-threaded asynchonous approach, then call the ReadFile() or ReadFileEx() and pass an OVERLAPPED structure. The structure will provide a callback function that will be called once the file read is complete.
However for the callback function to be called, the calling thread (the one that called ReadFile() or ReadFileEx()) has to be in an alertable state.
You just use one of the wait functions to suspend the thread and wait for the read function to call the callback function.