My win32 C++ application frequently checks a file that normally resides on a network share [in our corporate network]. But if the computer isn’t connected to a network, the application freezes for several minutes and the user usually has to end the process. How can I check if the file is accessible before I open it?
Checking if any network connection exists may not be good enough. The reason the users disconnect is to use to a test network where the file does not exist.
I think a thread is your best option.
There does not appear to be a way to invoke the CreateFile API asynchronously. Once a file handle is open you can do asynchronous I/O on it, but the act of opening it is still synchronous.
You will have to manage the synchronization with the thread yourself since only your app knows when it needs the result of the fopen, and it may still end up blocking at that point.