`DokanInstanceBuilder.Build` returns before file system is ready
Trying to access the new file system immediately after it is built fails with ERROR_INVALID_FUNCTION or ERROR_INVALID_PARAMETER (depending on type of access).
IMHO, Build by default should wait for the file system to be ready.
Workaround
const uint ERROR_INVALID_PARAMETER = 0x0057;
bool done = false;
while(!done) {
try {
mountDir.EnumerateFileSystemInfos().Any();
done = true;
} catch (IOException e)
when (GetWin32Error(e.HResult) == ERROR_INVALID_PARAMETER) {
await Task.Delay(TimeSpan.FromMilliseconds(25), cancel)
.ConfigureAwait(false);
}
}
static uint GetWin32Error(int hResult)
=> unchecked((uint)hResult) & 0xFFFF;
See also https://github.com/dokan-dev/dokan-dotnet/discussions/356
This might be due to DokanCreateFileSystem returning before DispatchDedicatedIoCallback worker threads (which fully enable the device when they first reach the kernel and register themselves).
We would need to add a logic that they signal they get there.
https://github.com/dokan-dev/dokany/blob/69e3d88c693da08fc1e9c540c162877880eac155/dokan/dokan.c#L824-L826
Moving to dokany repository since it is a native improvement.