User Tools

Site Tools


k11:k11.5:k11.5.0.2:start

Often there is a need to use console programs for special tasks in Gambas. For this purpose, Gambas has the class Process with the instructions EXEC as well as SHELL. Detailed descriptions - including process error management - can be found in the online Gambas book at https://www.gambas-buch.de/doku.php?id=k21:start in chapter `21.0 Process Management`. If you have created your own process with the instructions SHELL or EXEC, you do not read error data directly from the standard error output of the process, but get the error data already in the parameter of the event handler

Process_Error(sErrorMessage As String)

if error data is present at the standard error output of the process. Whether you output the error message unfiltered or still provide an error handling routine to influence the programme flow depending on the displayed error is certainly determined by the task to be solved.

A simple GUI project for the console program `ping` is used to demonstrate how you can catch and display possible errors with the event handler ProcessEventName_Error(sError as String).

The source code extract (ping project) shows the relevant variables and procedures:

Private $hPing As Process
Private sProgramName As String = "ping" ' ping6 - für IPv6-Adressen
 
...
 
Public Sub btnPingAsProcessStart_Click()
  PingAsProcess()
End
 
Public Sub PingProcess_Error(sFehler As String)
  txaOutput.Insert("Process error!" & gb.NewLine & gb.NewLine)
  txaOutput.Insert(sFehler)
End
 
Public Sub PingProcess_Read()
 
  Dim sPuffer As String
 
  Read #$hPing, sPuffer, Lof($hPing)
  txaOutput.Insert(sPuffer)
 
End
 
Private Sub PingAsProcess()
 
  Dim aCommand As New String[]
 
  If $hPing Then $hPing.Kill()  
  aCommand = [sProgramName, "-c", CInt(txbPingCount.Text), txbURL.Text]
  $hPing = Exec aCommand For Read As "PingProcess"
  FMain.Text = "Program ping: PID = " & $hPing.Id
 
End 

Now you can use the event handlers PingProcess_Read() and PingProcess_Error(…) to read the relevant data from this process. This assumes that you have given the process its own name. The name “PingProcess” is a good choice. If this name is missing, no events are triggered by the process!

As an example, 4 attempts are presented to you for the ping project, which achieved different results with different addresses and different numbers of pings. The first and the last attempt were error-free - even though the mail server mx.freenet did not answer. In contrast, the second and third attempts ended with a fatal error.

V1: Address: www.gambas-buch.de, Ping: 2 - error-free

PING www.gambas-buch.de (109.237.140.40) 56(84) bytes of data.
64 bytes from alfa3035.alfahosting-server.de (109.237.140.40): icmp_seq=1 ttl=59 time=39.9 ms
64 bytes from alfa3035.alfahosting-server.de (109.237.140.40): icmp_seq=2 ttl=59 time=39.4 ms
--- www.gambas-buch.de ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 39.420/39.680/39.940/0.260 ms

V2: Address: www.gambas+buch.de, Ping: 2

Process error!
ping: unknown host www.gambas+buch.de

V3: Address: www.gambas-buch.de, Ping: 0

Process error!
ping: bad number of packets to transmit.

V4: Address: mx.freenet.de, Ping: 2 - error-free

PING mx.freenet.de (195.4.92.212) 56(84) bytes of data.
--- mx.freenet.de ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1011ms

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k11/k11.5/k11.5.0.2/start.txt · Last modified: 28.09.2023 by emma

Page Tools