Launching a program

BlitzMax Forums/BlitzMax Tutorials/Launching a program

EOF(Posted 2005) [#1]
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):



Will(Posted 2006) [#2]
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.


DannyD(Posted 2006) [#3]
looks like it will


Nice code,thanks.


Pit-le-rouge(Posted 2006) [#4]
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 ?


Blitzplotter(Posted 2006) [#5]
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....


Playniax(Posted 2009) [#6]
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?


Dabhand(Posted 2009) [#7]
OpenURL("C:\WINDOWS\Notepad.exe")


Look at OpenURL... That might do ya! ;)

Dabz


Playniax(Posted 2009) [#8]
Ofcourse, thnx