Launching a program

BlitzMax Forums/BlitzMax Tutorials/Launching a program

This tutorial tackles the handling of launching other programs from within your own application.

I use Simon Armstrongs FreeProcess library (included with BlitzMax v1.12 onwards) which features these benefits:

* Launch an external program and continue using your application normally
* Close the launched program via your application
* Automatically close the launched program when you quit your application
* Only allow one instance of the program to run



The library
_________________________________________

Include Simons library in your source:
Import PUB.FreeProcess 



The TProcess variable type
_________________________________________
Create a TProcess variable which will be used to handle/monitor a launched application:
Local proc:TProcess



Launching an application
_________________________________________
Now you are ready to launch an application.
Let's open the Windows Notepad:
proc=TProcess.Create("Notepad",0)



Did it launch?
_________________________________________
To check if the application launched sucessfully:
If proc.Status()
  Notify "Notepad is up and running!"
EndIf



Closing the application
_________________________________________
There are three ways to close the previously launched application:

1) Close it from within your program:
' force the launched application to close
proc.Terminate

2) Wait for the user to close it:
' check if the user has closed the application
If proc.Status()
 Notify "Notepad is still running"
EndIf

3) Let Max close it when you end your program.



Quick example
_________________________________________
This snippet opens NotePad for 2 seconds then closes it automatically:

' open NotePad for 2 seconds

Import PUB.FreeProcess

Local proc:TProcess=TProcess.Create("notepad",0)
If proc.Status()
	Delay 2000
	proc.Terminate
Else
	Notify "Notepad failed to launch!"
EndIf
End



Putting it all together
_________________________________________
Here is a bigger demonstration which presents you with a simple GUI interface for selecting and running your own application. It also monitors whether or not the application is still alive (via a timer):
' Application Launcher demo - Jim Brown

' we need Simons library
Import PUB.FreeProcess

AppTitle$="Max App Launcher"

' simple gui
Global win:TGadget=CreateWindow(AppTitle$,60,60,180,160,0,9)
Local but1:TGadget=CreateButton("Run applicaton",24,24,114,24,win,1)
Local but2:TGadget=CreateButton("Terminate application",24,56,114,24,win,1)

' variable to handle the launched application
Local myproc:TProcess

' timer for updating the statusbar
Local t:TTimer=CreateTimer(1)

' cross-platform executable extension filter
?Win32
Const ext$="Executable:exe"
?Linux
Const ext$=""
?Mac
Const ext$=""
?


' =====================================

' main loop

Repeat
	' DebugLog CurrentEvent.ToString()
	Select WaitEvent()
		Case EVENT_GADGETACTION
		Select EventSource()
			Case but1
			If AppRunning(myproc)
				Notify StripDir$(myproc.name$)+" still running!"
			Else
				f$=RequestFile$("Choose Executable",ext$)
				myproc=RunApp(f$)
			EndIf
			Case but2
			If AppRunning(myproc)
				myproc.Terminate
			Else
				Notify "No program to terminate"
			EndIf
		End Select
		Case EVENT_TIMERTICK
		UpdateStatusBar myproc
		Case EVENT_WINDOWCLOSE
		Exit
	End Select
Forever

End

' =====================================

' launch an application
Function RunApp:TProcess(app$)
	If app$="" Return Null
    Local proc:TProcess=TProcess.Create(app$,0)
    If Not Proc Notify "Failed to launch "+app$
	Return proc
End Function

' return true if app still running
Function AppRunning(p:TProcess)
	If p<>Null
		If p.Status() Return True
	EndIf
End Function


' update the status bar to reflect 'app running' status
Function UpdateStatusBar(p:TProcess)
	If p=Null
		SetStatusText win,"App not running"
		Return
	EndIf
	If p.Status()
		SetStatusText win,StripDir$(p.name$)+" running"
	Else
		SetStatusText win,"App terminated"
	EndIf
End Function


Will this work on other platforms than windows? If so, how do you specify the application to be opened, since there can be different applications with the same name on other platforms.

looks like it will
' cross-platform executable extension filter
?Win32
Const ext$="Executable:exe"
?Linux
Const ext$=""
?Mac
Const ext$=""
?


Nice code,thanks.

Hi DannyD.

I try the lauching of an application with the example above on Mac OS X.
I added your code but... nothing...
the status immediately change from running to terminated...

Any idea about this issue ?
Did you try the example above on your Ibook ?

Hi, your exe launcher works a treat... How can I use it to run a B3D exe within the same directory as my BMax App - Thx....

Hi, works great! BUT I need a way to launch a program that still runs when I close the app. I know bout system_ and I found a win32 way but I would like to have a way that works on all platforms. I try to avoid platform specific ways. Anyone?