none
Questions about filestream and streamwrite's WriteAsync? RRS feed

  • Question

  •  FileStream sourceStream = new FileStream(filePath,
                            FileMode.Append, FileAccess.Write, FileShare.None,
                            bufferSize: 4096, useAsync: true);

    The FileStream class uses the writeasync method, which does not actually use another thread. His working mechanism is:
    Write data from the memory to the system cache, if a large amount of data is written at a time, it may be blocked,

    However, if you use streamwrite wrap filestream and call writeasync,

       FileStream sourceStream = new FileStream(filePath,
                            FileMode.Append, FileAccess.Write, FileShare.None,
                            bufferSize: 4096, useAsync: true);
                        StreamWriter sw = new StreamWriter(sourceStream);
    However, if you use streamwrite wrap filestream and call writeasync,
    Will not cause obstruction, is this because streamwrite writeasync with another thread to call filestream writeasync method?


    please verify my account

    Wednesday, December 6, 2017 5:41 AM

Answers

  • Internally StreamWriter uses a Task to implement its WriteAsync irrelevant of whether the underlying Stream supports it or not.

    Remember that a stream is an arbitrary stream of bytes. Different streams support different functionality. FileStream can rely on the OS optimizations for writing data so using a Task (at least right now, isn't necessary). StreamWriter works with any stream and therefore cannot make any assumptions. Thus it always uses a Task when calling WriteAsync.  For streams that implement their WriteAsync logic using a task you'd actually end up using 2 tasks (which is fine).

    I recommend that if you want to see how/why a framework type behaves the way it does that you consider looking at the reference source. To make it easier in VS you can install the Ref12 extension which allows you to press F12 on a type and jump to reference source if it is provided by the framework.


    Michael Taylor http://www.michaeltaylorp3.net


    Wednesday, December 6, 2017 2:38 PM
    Moderator

All replies

  • Internally StreamWriter uses a Task to implement its WriteAsync irrelevant of whether the underlying Stream supports it or not.

    Remember that a stream is an arbitrary stream of bytes. Different streams support different functionality. FileStream can rely on the OS optimizations for writing data so using a Task (at least right now, isn't necessary). StreamWriter works with any stream and therefore cannot make any assumptions. Thus it always uses a Task when calling WriteAsync.  For streams that implement their WriteAsync logic using a task you'd actually end up using 2 tasks (which is fine).

    I recommend that if you want to see how/why a framework type behaves the way it does that you consider looking at the reference source. To make it easier in VS you can install the Ref12 extension which allows you to press F12 on a type and jump to reference source if it is provided by the framework.


    Michael Taylor http://www.michaeltaylorp3.net


    Wednesday, December 6, 2017 2:38 PM
    Moderator
  • Hello lctk,

    Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Thank you for your understanding and patience!

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, December 24, 2017 7:55 AM
    Moderator