[Gambas-user] Monitoring a process... Need help
Benoit Minisini
gambas at ...1...
Mon Feb 4 16:46:03 CET 2008
On lundi 4 février 2008, M0E Lnx wrote:
> I'm trying to write a function to execute several shell commands in a
> given order. For instance, extract a compressed tarball, and then cd
> to it's extracted dir, and do "ls -al"
>
> Basically, what I want to do is execute several shell commands.
> I want to use One process for all shell commands instead of using one
> process for each command.
> I want to use one timer to monitor this one process... and as soon as
> it reports a Process.Stopped event, do some error checking, and if
> passed, execute the second command using the same Process variable i
> defined.
>
> The module file looks like this
>
> MdlShell
> Public hShellProc as Process
> Public iMdlStep as integer
> PUBLIC FUNCTION Extract(sPath as string, iFuncStep)
>
> DIM ShellLine as string
>
> ' The following commands will be triggered from a timer event in another
> form
>
> SELECT CASE iFuncStep
> CASE 0 ' First step
> ShellLine = "cd /tmp && tar xf " & sPath & " || echo \'ERROR\'"
>
> CASE 1 ' Second step
> shellLine = "cd /tmp && ls -a || echo \'ERROR\'"
>
> ' And the list would continue here
>
> END SELECT
>
> hShellProc = shell ShellLine for read ' execute the selected command
> iMdlStep = iFunctStep ' Make the last step available publically
>
> On a separate form, I have a timer setup.
> On this timer setup... on it's timer event I have this
>
> TmShellMon_timer()
>
>
> If MdlShell.hShellProc.State = Process.Stopped then
> Me.TmShellMon.Enabled = false
> ' Do some error checking here
> if instr(sBuffer, "ERROR") > 0 then
> STOP EVENT
> Else
>
> ' If no error is found then execute this
> SELECT CASE MdlShell.iMdlStep
>
> CASE 0 ' First step has ran... trigger next
> MdlShell.Extract(sPath, 1)
> CASE 1 ' Trigger 3rd step
> MdlShell.Extract(sPath, 2)
> ' And so on....
> END SELECT
> END IF
> END IF
>
> The problem With this is... for some reason it's not monitoring the
> process right... It's triggering a lot of the lines in the wrong
> sequence...
>
> Can anyone think of a way to fix this or a better way to do this?
> Please help
>
As you don't give all your code, we can't guess what happens exactly.
Anyway, a few remarks:
- You always use one different process for each command, even if you store it
in the same variable.
- To know when a process has stopped, you should use the "Kill" event, not a
timer.
- STOP EVENT does nothing in a timer event.
--
Benoit Minisini
More information about the User
mailing list