Main Site Documentation

Question about Streams - DataWriter DataReader


#1

My question is about the purpose / usage of writer Flush();
Not sure when to use and/or how it is used.

    // Call method CharacterStringWriter("\r\n! Testing CharacterStringWriter:\r\n");

    private static void CharacterStringWriter(string s)
    {
        DataWriter _writer = new DataWriter(uart1.OutputStream);

        //char[] arr = s.ToCharArray();
        //int length = s.Length;

        byte[] array = Encoding.UTF8.GetBytes(s);

        //uint howmany = _writer.UnstoredBufferLength; //Get

        _writer.WriteBytes(array); // byte[36]
        
        // Does not clear the array or write to the terminal
        //_writer.Flush();

        _writer.Store(); // This will display the entire array

        // Writes to Tera Term VT
        // ! Testing CharacterStringWriter:
        // Tera Term cursor is shown on the line following the array string
    }
    //

Mostly qurious about it’s usage.

Thanks in advance.


#2

basically you use Flush after Store but its generally recommended to avoid this method as it may produce latencies and does not always guarantee durable and coherent storage of data
i would call it API relict …


#3

@willgeorge:
writing to a file in small portions would lead to high harddisk activity. So the OS stores the data in memory and writes them to the HD following certain rules like availability of the disk, amount of collected data and others.
Flush forces the OS to write all data immidately. Without Flush you can even have data losses under some circumstances as the program terminates (e.g. crash) without having written everything.
Regarding TinyCLR I don’t know how this is handled there, but you could increase the writing speed for a SD - Card with this. If this is done in TinyCLR - I don’t know. Maybe it’s only for compatibility reasons.