KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Asynchronous LEP and file-based Communications on Windows
PRODUCT: 4D | VERSION: 13.x | PLATFORM: Win
Published On: June 11, 2015

Here is an example of using files on disk to communicate between an external process and 4D:

1) Create a batch file named myBatch.bat next to a structure file and put the following into it:

echo 1 > in_progress
ping 127.0.0.1 -n 10
del in_progress


The batch file will create a file named in_progress with the contents of 1.
Then ping the loopback address (127.0.0.1) 10 times.
Finally the batch file will delete the file named in_progress as it completes.

2) Create a method in the 4D Structure file with the following contents:

C_LONGINT($a;$winRef;$SH;$SW;$WH;$WW)
C_TEXT($in;$out;$err;$folder;$inProgress_t)
$folder:=Get 4D folder(Database Folder)
$inProgress_t:=$folder+"in_progress"
$SW:=Screen width\2
$SH:=(Screen height\2)
$WW:=200\2
$WH:=50\2
$a:=0
$winRef:=Open window($SW-$WW;$SH-$WH;$SW+$WW;$SH+$WH;Plain fixed size window;"LEP")
SET ENVIRONMENT VARIABLE("_4D_OPTION_CURRENT_DIRECTORY";$folder)
SET ENVIRONMENT VARIABLE("_4D_OPTION_HIDE_CONSOLE";"true")
SET ENVIRONMENT VARIABLE("_4D_OPTION_BLOCKING_EXTERNAL_PROCESS";"false")
LAUNCH EXTERNAL PROCESS("cmd.exe /C myBatch.bat > out.txt 2> err.txt")
DELAY PROCESS(Current process;1)
While (Test path name($inProgress_t)=Is a document)
   GOTO XY(0;0)
   If (Mod($a;2)=0)
      MESSAGE("operation in progress ")
   Else
      MESSAGE("operation in progress_")
   End if
   $a:=$a+1
   DELAY PROCESS(Current process;10)
End while
CLOSE WINDOW($winRef)
ALERT("done")


3) Run the 4D method.

When you run the 4D method a batch file will be executed. The console window is being hidden and the standard output of the batch file is being redirected to a file named out.txt while the standard error is redirected to err.txt. As the batch file starts it will create a file named in_progress with the contents of 1 and it will delete the file named in_progress as the batch file completes. While the file on disk exists the 4D code is waiting in a while loop and displaying a message to indicate an operation is in progress. Once the in_progress file is removed from disk the 4D code continues running and the message window is removed.

* This example can be improved by examining / processing the out.txt and err.txt files



In addition to demonstrating file-based communication, this example also demonstrates console redirection (a feature of cmd.exe) to redirect the stdOut and stdErr to files. You can test that using this from a command prompt:
myBatch.bat > out.txt 2> err.txt


the out.txt file would contain the std out.
the err.txt file would contain the std error messages.

Console Redirection is a feature of Windows and is documented here:
https://support.microsoft.com/en-us/kb/110930

We also discuss console redirection here:
http://kb.4d.com/assetid=75938