I came across the following question when trying to determine if I was using the Stream
methods such as ReadAsync
and CopyToAsync
correctly:
C# 4.5 file read performance sync vs async
In this question I read the following in the accepted answer:
Most notably, your "async" test does not use async I/O; with file
streams, you have to explicitly open them as asynchronous or else
you're just doing synchronous operations on a background thread.
In his asynchronous IO code he was using the following to open the FileStream
'asynchronously':
var file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true)
So I was wondering if you intend to use methods such as CopyToAsync
whether you should open the underlying FileStream
as shown above?, as opposed to doing something simple like the following:
File.Open(filename, FileMode.Open)
Which is how the example in the actual documentation for CopyToAsync
demonstrates opening the underlying FileStream
:
https://msdn.microsoft.com/en-us/library/hh159084(v=vs.110).aspx
If it does not matter which way the underlying FileStream
is opened, what does the useAsync
parameter of the FileStream
constructor do?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…