Well, for a start, there's no point in TMyFileStream
. What you are looking for is THandleStream
. That class allows you to supply a file handle whose lifetime you control. And if you use THandleStream
you'll be able to avoid the rather nasty hacks of your variant. That said, why are you even bothering with a stream? Replace the code that creates and uses the stream with a call to SetFilePointer
to seek to the end of the file, and a call to WriteFile
to write content.
However, even using that, your proposed solution requires further synchronization. A single windows file handle cannot be used concurrently from multiple threads without synchronisation. You hint in a comment (should be in the question) that you are serializing file writes. If so then you are just fine.