Results 1 to 3 of 3

Thread: Thread Safe File Operations - Files With Forms

  1. #1
    Join Date
    Jun 2012
    Posts
    586
    Mentioned
    112 Post(s)
    Quoted
    296 Post(s)

    Default Thread Safe File Operations - Files With Forms

    As @rj; pointed out here, many file functions can not be used with forms. This is because these functions are not thread-safe.

    File.simba, attached, solves this issue by overriding all file functions, such as OpenFile(), CreateFile(), WriteFileString(), etc using a TFileStream.

    Examples:

    Simba Code:
    _Handle := CreateFile('C:\Users\****\Desktop\Hello world.txt');

    If _Handle <> -1 Then
    Begin
      WriteFileString(_Handle, 'Foo bar');
      CloseFile(_Handle); //~ Remember to always close your file!
    End;

    Simba Code:
    _Handle := OpenFile('C:\Users\****\Desktop\Hello world.txt');

    If _Handle <> -1 Then
    Begin
      ReadFileString(_Handle, _String, FileSize(_Handle));
      WriteLN(_String);
      CloseFile(_Handle);
    End;

    Simba Code:
    _Handle := RewriteFile('C:\Users\****\Desktop\Hello world.txt');

    If _Handle <> -1 Then
    Begin
      WriteFileString(_Handle, 'http://villavu.com');
      CloseFile(_Handle);
    End;

    Simba Code:
    _Handle := OpenFile('C:\Users\****\Desktop\Hello world.txt');

    If _Handle <> -1 Then
    Begin
      _Size := FileSize(_Handle);
      WriteLN('The file size is: ', _Size);
      CloseFile(_Handle);
    End;

    Etc...



    Thought it might be handy. Could be a hell of a lot shorter (and better written), but I've got better things to work on. .

    Edit: I should mention that I haven't really tested it, but I don't see why it wouldn't work. Main thing I haven't tested is having multiple files open at once, and closing them, etc.
    Last edited by Obscurity; 02-12-2016 at 10:27 PM.




    Skype: obscuritySRL@outlook.com

  2. #2
    Join Date
    Jan 2012
    Location
    East Coast
    Posts
    733
    Mentioned
    81 Post(s)
    Quoted
    364 Post(s)

    Default

    Quote Originally Posted by Obscurity View Post
    I've got better things to work on.
    Get to work son.

  3. #3
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Aww what a cute include i'll use this

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •