Read From File Locking Up Whole Stack

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
SirWobbyTheFirst
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 246
Joined: Tue Jun 30, 2009 11:15 pm

Read From File Locking Up Whole Stack

Post by SirWobbyTheFirst » Tue Jan 06, 2015 9:59 pm

So I've been tinkering with a way to copy files of various sizes without locking up the entire UI of LiveCode and so far I have gotten part way near to it with the idea to divide the file size up into chunks of 512, so we take the file size in bytes and divide it by the size of the chunk (i.e. 1024 / 512) and then I've got a Repeat loop that copies 512 bytes of the file using the Read From File command and then writes those 512 bytes to the destination file.

And this works well with smallish files (I'm not too sure of a limit, but I tried it out on a couple of Windows EXE files that are a couple hundred KB in size and it worked fine) but upon feeding it a just a lowly 16 meg DLL file it just face planted into the ground with the force of an asteroid. But the stack did copy the file, it took a while but it kept on running, just the UI locked up and after a couple of minutes the UI came back to life and the file was copied.

I used both the code sections below (The _CopyChunk message does the same Read From File and Write To File stuff as the first code section does) and both result in the UI locking up.

Original Code Used:

Code: Select all

If tChunkIndex < tNumberOfFileChunks Then
   Read From File pSourceFilePath For 512 Bytes
Else Read From File pSourceFilePath Until EOF
Put It Into tFileChunk
Write tFileChunk To File pDestinationFilePath
If pMessage_ChunkCopied <> Empty Then
   Dispatch pMessage_ChunkCopied To tTargetObject With tChunkIndex
End If
If tChunkIndex < tNumberOfFileChunks Then
   Add 1 To tChunkIndex
Else Exit Repeat
Revised Code:

Code: Select all

Put False Into tChunkCopyStatus
If tChunkIndex < tNumberOfFileChunks Then
   Send ("_CopyChunk" && pSourceFilePath & Comma & pDestinationFilePath & Comma & False) To Me In 0 Seconds
Else Send ("_CopyChunk" && pSourceFilePath & Comma & pDestinationFilePath & Comma & True) To Me In 0 Seconds
Wait While tChunkCopyStatus = False With Messages
If pMessage_ChunkCopied <> Empty Then
   Dispatch pMessage_ChunkCopied To tTargetObject With tChunkIndex
End If
If tChunkIndex < tNumberOfFileChunks Then
   Add 1 To tChunkIndex
Else Exit Repeat
So can anyone shed some light on ways I can do this whilst keeping the UI alive enough so that Windows does not state it is not responding. I know LiveCode does not have any multi-threading and thus this is basically an attempt to get around the single threaded nature of the engine. I seem to have been the only one begging for multi-threading to be apart of the new engine revamp, however I am unsure whether that has fallen on deaf ears.

TL;DR Read From File whitey's at anything more than a couple hundred kilobytes of data.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Read From File Locking Up Whole Stack

Post by jacque » Wed Jan 07, 2015 8:10 pm

Both versions are blocking because neither give the engine time to do anything. Don't use "dispatch", use "send in time". That's how LC simulates threading.

Code: Select all

send "pMessage_ChunkCopied tChunkIndex" to me in 5 milliseconds -- adjust timing if needed
Alternately you can just add a "wait 1 with messages" command inside the handler. That gives the engine time to do things. But if you haven't yet tried revCopyFile, I'd try that first. I think it is still blocking, but it may be faster than other methods.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

SirWobbyTheFirst
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 246
Joined: Tue Jun 30, 2009 11:15 pm

Re: Read From File Locking Up Whole Stack

Post by SirWobbyTheFirst » Fri Jan 09, 2015 12:24 am

Hi Jacque, I used your Wait 1 With Messages line in my original test stack and it was working fine yesterday however upon integrating the full copy file function into my main project, it just outright refuses to listen to that line and what is more is that if I load up my test stack, the line is also ignored by the engine in that stack. If I restart LiveCode and then load my test stack, the line works fine but if I load my main project up, it goes into ignorance mode again in both my main project and the test stack.

I have no idea what is wrong but it is incredibly frustrating, the revCopyFile also does no good because it just spawns a shell copying the file which results in the UI completely locking up until the shell has exited and in all honesty, I care more about threading support than unicode support.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Read From File Locking Up Whole Stack

Post by jacque » Fri Jan 09, 2015 7:49 am

There isn't enough of your original script to know exactly what you're doing. For example, there's an "end repeat" but no initial "repeat" command, but when using the "send" structure you don't need a repeat anyway. Also, the syntax of the "send" wasn't quite right.

Here's the basic idea of what you need to do, I think:

Code: Select all

on copyFile
  put "path/to/source/file.png" into tFromFile
  put "path/to/destination/file.png" into tToFile
  open file tFromFile for binary read
  if the result <> "" then bail "Could not open" && tFromFile
  open file tToFile for binary append
  if the result <> "" then bail "Could not open" && tToFile
  copyChunk tFromFile,tToFile
end copyFile

on copyChunk pFromFile,pToFile
  read from file pFromFile for 512
  if it = "" or the result = "eof" then
    close file pFromFile
    close file pToFile
    exit copyChunk
  end if
  write it to file pToFile
  send "copyChunk pFromFile,pToFile" to me in  100 milliseconds
end copyChunk

on bail pMsg
  answer pMsg
  exit to top
end bail
You don't need to keep track of the number of chunks that have been read, just check to see if "it" is empty or the result is "eof". Put in the real file paths where I have fake placeholders in the copyFile handler. Also, LiveCode is pretty fast when reading files so you may be able to increase the number of bytes read per chunk without causing too much lag, and you might want to tinker with the number of milliseconds between sends.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm

Re: Read From File Locking Up Whole Stack

Post by zaxos » Fri Jan 09, 2015 11:50 am

Hi there my friend. I had the exact same problem and my solution was making a seperate(invisible) stack that does all the background job and reports back into a txt and then the interface stack that get the contents of the txt and displays them.I dident use revCopy tho, i used Robocopy process(only windows). Hope i helped.
Knowledge is meant to be shared.

Post Reply