Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a file in windows asynchronously

Is there a way to open a file in Windows asynchronously? The CreateFile API function has only FILE_FLAG_OVERLAPPED which allows for further asynchronous reads and writes. Nonetheless, the opening of the file seems to be synchronous. Given, it has to access the file system (and potentially perform expensive IO operations), it may be a potential blocker.

This is actually an underlying question to, whether it is possible to open a file in .NET asynchronously (as FileStream ctor cannot be awaited). But the question is rather pointless if there is no way to do it in the OS.

like image 284
Elephantik Avatar asked Dec 23 '16 11:12

Elephantik


People also ask

What is asynchronous file?

Asynchronous operations enable you to perform resource-intensive I/O operations without blocking the main thread. This performance consideration is particularly important in a Windows 8.

When to use asynchronous programming?

Consider using asynchronous programming if you have many tasks for the application to run. This way, at least one or more tasks make progress because they don't wait for one another in an interconnected fashion.

What is async await in C#?

An async keyword is a method that performs asynchronous tasks such as fetching data from a database, reading a file, etc, they can be marked as “async”. Whereas await keyword making “await” to a statement means suspending the execution of the async method it is residing in until the asynchronous task completes.

Which class contains methods like CopyToAsync ReadAsync WriteAsync?

Stream class contains methods such as CopyToAsync, ReadAsync, and WriteAsync alongside the synchronous methods CopyTo, Read, and Write.


2 Answers

Unfortunately there is no way in user mode to create/open a file asynchronously. Even if the driver returns STATUS_PENDING for IRP_MJ_CREATE, the system will be waiting in this case until the driver completes the IRP before it returns control from one of the create/open file functions.

Only if we are in kernel mode it's possible, if you yourself format IRP_MJ_CREATE and send it to the driver. But even in this case the drivers will almost always handle IRP_MJ_CREATE synchronously.


for API be asynchronous - must be some way notify caller when operation finished

windows used 3 ways for this

  1. some callback routine in parameters, usually APC (PIO_APC_ROUTINE) which called when operation finished
  2. some Event in parameters, when operation finished, Event set in signal state.
  3. file handle, used in api call, is binded for some IOCP. when operation finished packet is queued to IOCP. (we remove this packet later by call GetQueuedCompletionStatus (ZwRemoveIoCompletion) or KeRemoveQueue

3) is impossible in our case because file handle yet not created, so it can not be bind to any IOCP. about 1) and 2) let looks for file open/create api signatures:

in user mode the lowest level api for open/create file is ZwOpenFile and ZwCreateFile. CreateFile is shell over ZwCreateFile. in kernel mode NtOpenFile -> NtCreateFile -> IoCreateFile -> IoCreateFileEx even - IoCreateFileEx (the most low level api for create file) - have no Event or [Apc] callback parameter - so not asynchronously. IoCreateFileEx call ObOpenObjectByName (not documented, but exported routine) - here also no 1) or 2) parameters - again this is synchronous by design api

like image 143
RbMm Avatar answered Oct 04 '22 00:10

RbMm


Is there a way to open a file in Windows asynchronously?

No, unfortunately. Interestingly, at the device driver level, all such interaction is asynchronous (or can be asynchronous, at least). But that is not exposed at the Win32 level.

The UWP asynchronous file-open APIs are just fake-asynchronous APIs - they delegate synchronous work to the thread pool. They're not truly asynchronous. You'll need to do the same thing if you want non-blocking file-opens on .NET (which is often desirable if you're dealing with files over a network share).

like image 30
Stephen Cleary Avatar answered Oct 04 '22 00:10

Stephen Cleary