The ReadMe...
The code...Very simple pipe example.
This shows how to open a pipe file and monitor "Watch" it for incoming text.
(Without comments it's just 20 lines of code)
Written by Bruce Steers, Fully commented to explain the workings.
Once the pipe is opened the app continues to run and will react to any text sent to the pipe.
Pipes can have many uses especially in being able to control/communicate to your application form an outside source.
See My program "GForm" (an interactive shell GUI builder) http://gform.bws.org.uk (advanced)
to see how 2 pipes can be used for 2 way communication between a
shell script and a gambas application.
More info in the source code.
' Gambas class file
Public hFile As File ' Declare pointer to the pipe file handle (needs to be global)
' This routine is called when the window is opening.
Public Sub Form_Open()
' opening a pipe will lock the application until it gets some text so before
' opening it we 'echo' a blank line to it using the Shell command.
Shell "echo '' >/tmp/FIFO1"
' Now we open the Pipe file for Reading asigning it to hFile and use the "Watch" arg
' using "Watch" makes it trigger the File_Read() event if it recieves a line of text.
hFile = Pipe "/tmp/FIFO1" For Read Watch
' Pipe is opened and our "echo" command has unlocked it and allowed our application
' To get To this point so alert the user what's going on before opening the main window.
Message("Pipe is opened and this application is now\nfree to continue running while listening for\ntext from the pipe in the background.", "Show Me")
End
' This File_Read() event is trigered when a line of text is sent to the pipe file /tmp/FIFO1
Public Sub File_Read()
Dim sMsgText As String
' Here we ReadLine from the hFile pointer While not at the Eof (End of file).
While Not Eof(hFile)
sMsgText = Trim(hFile.ReadLine())
' If the text is not a blank line then react to it...
If sMsgText <> "" Then TextArea1.Text = "\nReceived text from the pipe...\n\n'" & sMsgText & "'"
Wend
End
' On closing the application window this Form_Close() routine is called so
' here we can make sure the pipe file is closed and deleted.
Public Sub Form_Close()
If hFile Then hFile.Close()
Kill "/tmp/FIFO1"
End
' the "Close" button triggers this event.
Public Sub btnClose_Click()
Me.Close()
End