Missing asynchronous message overload?

Looking at the System.IO.File class, for example, I see the Exists static method, but I don't see any ExistsAsync . I suspect that File.Exists may be blocked for some time if the file in question affects, for example, a network resource. Of course, I could always use Task.Run , but it will not use I / O completion ports.

I could ask the same about many other static methods of the File class.

+4
source share
1 answer

I do not know why the File.ExistsAsync method does not exist. It is possible that β€œanother process can potentially do something with the file between the moment you call the Exists method and perform another operation on the file, for example,β€œ Delete ”, and that to exclude the correct function, the application that accesses the existing file.

In any case, you can write your own.

 public static async Task<bool> FileExistsAsync(string file) { return await Task.Factory.StartNew(() => File.Exists(file)); } 

... which, of course, does not use IO Completion to get asynchronous I / O ...

Update: I think the file is usually a handy wrapper. You can do almost everything File offers using other APIs that offer asynchronous features. In the case of Exists it does not use anything that could use IO I / O ports, it just calls FindFirstFile and checks for an error.

+2
source

Source: https://habr.com/ru/post/1435665/


All Articles