Is file I/O blocking?

  1. last week

    Eric W

    Sep 10 Pre-Release Testers, Xojo Pro
    Edited last week

    In another thread , James Sentman said:

    I’m fairly sure that file access is blocking for all threads until it’s done

    Is this true?

    If it is, might reading file data in through a Shell program's stdOut be the answer? If so, does the Shell need to be in asynchronous mode or will threads support Shell.Result returns in a non-blocking manner?

  2. Thom M

    Sep 10 Pre-Release Testers Greater Hartford Area, CT

    Definitely true. A common workaround is to read or write in chunks.

  3. Christian S

    Sep 12 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    If needed, I could provide functions for MBS Plugins to do asynchronous read/write or thread friendly read/write.

  4. Kevin G

    Sep 12 Pre-Release Testers, Xojo Pro Gatesheed, England

    I logged an enhancement for non-blocking disk IO last year:
    Feedback Case #52196

  5. Richard D

    Sep 12 Pre-Release Testers, Xojo Pro Europe (UK, London)

    @Thom M Definitely true. A common workaround is to read or write in chunks.

    what do i need to do to accomplish that??

  6. Beatrix W

    Sep 12 Pre-Release Testers, Third Party Store Europe (Germany)

    Cough... a nonblocking FileListMBS would be really helpful. I sometimes need to iterate over 100k files. While FileListMBS is very fast it shows the beloved spinning pizza.

  7. Christian S

    Sep 12 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    I can add a yield parameter to FileListMBS class.

  8. Thom M

    Sep 12 Pre-Release Testers Greater Hartford Area, CT

    @Richard D what do i need to do to accomplish that??

    Here's how I do it:

    Dim Stream As BinaryStream = BinaryStream.Create(File, True)
    Dim CurrentThread As Thread = App.CurrentThread
    If CurrentThread = Nil Then
      Stream.Write(Contents)
    Else
      Const ChunkSize = 1024000
      For I As Integer = 0 To Contents.Size - 1 Step ChunkSize
        Dim Chunk As String = Contents.StringValue(I, Min(ChunkSize, Contents.Size - I))
        Stream.Write(Chunk)
        CurrentThread.Sleep(10)
      Next
    End If
    Stream.Close

    In this case Contents is a MemoryBlock. If called on the main thread, it just writes the entire contents at once, since adding a loop won't help you in any way and there's no thread to sleep. But if called on a thread, it writes in 1MB chunks and rests for a moment.

  9. Norman P

    Sep 12 Pre-Release Testers, Xojo Pro great-white-software.com/blog

    Almost seems that a "ThreadedWriter" which is just a subclassed thread that writes your file would be useful

  10. Kevin G

    Sep 12 Pre-Release Testers, Xojo Pro Gatesheed, England

    @Norman P Almost seems that a "ThreadedWriter" which is just a subclassed thread that writes your file would be useful

    This kind of solution only takes you so far though. We have a customer who's NAS is extremely saturated and performing a simple action on a FolderItem can block for several seconds. Luckily the project isn't too complicated so we have replaced most of our IO code with calls into the Win32 API.
    Real non-blocking IO / pre-emptive threads / a more efficient FolderItem would really help in a lot of scenarios.

or Sign Up to reply!