1

Please explain what will give the IsAsync = true property in FileStream. Documentation.

This variant uses this property

    await using FileStream fileStream = new(path, FileMode.Create, FileAccess.Write, 
                                                  FileShare.Read, 4096, true);
    await using MemoryStream memoryStream = new(bytes);
    await memoryStream.CopyToAsync(fileStream, cancellationToken);

In this version without it

await using FileStream fileStream = new(path, FileMode.Create, FileAccess.Write);
await using MemoryStream memoryStream = new(bytes);
await memoryStream.CopyToAsync(fileStream, cancellationToken);

How will the behavior change? It is not clear to me why I should use it at all if I make a memoryStream.CopyToAsync.

This property allows you to open a file in some special asynchronous thread, does it belong to some other thread pool?

If we are talking about the Web API, does this property give me an advantage similar to the usual asynchronous calls in which the main thread is released?

Web FR
  • 27
  • 4
  • 2
    Unless you explicitly specify `useAsync: true` in the ctor call-site the `FileStream`'s `...Async` methods will be "fake-async": that is, they'll block the thread just like the non-`...Async` methods, which you _don't wan't_ in ASP.NET (or server-side scenarios in general) because it means the system can't use that thread-block time _productively_ by re-using those threads to service other requests. – Dai Aug 22 '23 at 05:31
  • 4
    _"This property allows you to open a file in some special asynchronous thread, does it belong to some other thread pool?"_ - "`async`" does not mean that at all, see here: https://blog.stephencleary.com/2013/11/there-is-no-thread.html – Dai Aug 22 '23 at 05:32

0 Answers0