How can I make make the following code run asynchronously without having to create an extra thread on the thread pool (in other words without  Task.Run(...))?
Directory.CreateDirectory("\\host\someNetworkDirectory");
Ideally, I would like to use it like this:
async SomeMethod(){
    //...
    await Directory.CreateDirectoryAsync("\\host\someNetworkDirectory");
    // stuff to do after ensuring the directory exists
    //...
}
I tried this answer (which suggest using FileSystemWatcher) but it does not work for folders.
At the OS level, this function is strictly synchronous. WinAPI only has two functions for creating directories, CreateDirectory and CreateDirectoryEx. Neither of them accepts a callback.
Therefore, there is nothing you can do to avoid having to create a thread to make this asynchronous. Best you can hope for is to find a library which hides this away from you.
Look at the static Task.Run methods.
async SomeMethod() {
    //...
    await Task.Run(() => Directory.CreateDirectory("\\host\someNetworkDirectory"));
    //...
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With