The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,565 other followers

Delphi/Turbo Pascal: AppendWithRetry for text files to retry Append

Posted by jpluimers on 2014/03/10

Every once in a while you have multiple threads or processes wanting to write a short message to the same log file. Append then will give you an I/O error 32 (ERROR_SHARING_VIOLATION), but the below small routine will sleep a bit while retrying a couple of times.

It uses these Delphi aspects around the $I or $IOCHECKS compiler directive:

  • in $I+ mode, you get exceptions when certain “classic” Pascal style I/O operations fail.
  • in $I- mode, you access the IOResult to obtain the results of those I/O operations
  • IOResult gets the result of the last failed operation (if any) or zero if none failed
  • IOResult clears the underlying storage to zero
  • $IFOPT checks for a certain state of a compiler flag
  • You can store the state of $OPT in a temporary conditional define

Note there are a few tables of codes you can get back through IOResult as basically you can get many GetLastError results in IOResult as well:

I wish Embarcadero had done things like this to prevent Delphi IDE startup messages like these:

[RAD Studio]
The imported project file could not be loaded. The process cannot access the file ‘C:\Users\Developer\AppData\Roaming\Embarcadero\BDS\10.0\environment.proj’ because it is being used by another process. C:\Users\Developer\AppData\Roaming\Embarcadero\BDS\10.0\environment.proj.
[OK]

Anyway, here is the code:

–jeroen

via:

2 Responses to “Delphi/Turbo Pascal: AppendWithRetry for text files to retry Append”

  1. uligerhardt said

    I’m surprised that you haven’t been chastised yet for publicly mentioning old skool IO. :-)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: