[Gambas-user] Stopping a CLI program

Tobias Boege taboege at ...626...
Wed Dec 11 12:38:59 CET 2013


On Wed, 11 Dec 2013, John Rose wrote:
> Tobias,
> 
> Thanks for your example. I've now got the Exec's termination working OK. 
> Below is an extract from my code:
> Private aExec As String[]
> Private sOutput As String
> Private hffmpeg As Process
> hffmpeg = Exec aExec Wait For Input Output As "ffmpegMessages"
> Public Sub ffmpegMessages_Read()
>    Dim sLine As String
>    Read #Last, sLine, -256
>    sOutput &= sLine
> End
> Public Sub ffmpegMessages_Kill()
>    Print "ffmpeg Output = "
>    Print sOutput
> End
> Public Sub ButtonStop_Click()
>    If hffmpeg.State = Process.Running Then Print #hffmpeg, "q"
> 
> At run time:
> 1. aExec was equal to ["ffmpeg", "-f x11grab",.....]. Thus, the first 
> parameter was "-f x11grab") which was displayed by ffmpeg as 
> unrecognised (even though running "ffmpeg -f x11grab ......." is OK.
> The setting of the value in aExec was done by:
> aExec = ["ffmpeg"]
> aExec.Add("-f x11grab")
> etc
> I think that I've seen something like this before with Exec but I don't 
> remember the reason or solution. Any ideas?
> 

I'm tired of explaining this... Is the documentation really _that_ unclear?
You have to put one argument into one field of the array, thus:

["ffmpeg", "-f", "x11grab"]

is the correct array.

> 2. The Kill routine above was executed. At run time, there was an error 
> caused by the last line being:
> If hffmpeg and hffmpeg.State = Process.Running Then Print #hffmpeg, "q"
> So I removed the first hffmpeg & it was OK at runtime.
> 

Oh, there was an error? Your chances to not annoy people are better if you
also write down _what_ error it was. The idea is to save us the time to
write your code into one of our projects to see it...

The reason is that you wrote "hffmpeg and hffmpeg.State = Process.Running".
In Gambas, the And operator "pulls stronger" than the equals operator
because And is a binary operator in Gambas. So your code is equivalent to
(hffmpeg And hffmpeg.State) = Process.Running and hffmpeg is an object which
you cannot AND bitwise. Thus the error.

However, Gambas also knows some kind logical AND (which pulls weaker than
equals) with expressions which is called And If, so it would be correct to
write:

If hffmpeg And If hffmpeg.State = Process.Running Then ...

Attached is also a patch to the project I sent you because I am also prone
to this kind of error ;-)

> 3. I wanted to incorporate:
> Public Sub ffmpegMessages_Error(sError)
>    Print "ffmpeg Error = "
>    Print sError
> End
> but the compiler objected. Any ideas?

Same thing here... I am not a Gambas compiler so I may not see the error
immediately. At least write down the error you get so we get a hint!

However, I suspect the error is because sError didn't get any type. Have you
ever written a function in Gambas? Every argument needs a type, so write:

Public Sub ffmpegMessages_Error(sError As String)
  ' ...
End

Regards,
Tobi

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list