Skip to main content

Executing shell commands with wxWidgets with C++

ever wondered how to execute a shell comand via commandline that will not quit itself? i did have to get that to work and it took me quite a while. here my knowledge as its nowhere on the net so far:

i am talking about some command that will invoke something that will stay active as long you do not abort it (like with CTRL-C). Note: like a command that you, when in commandline type in, will not immediately return you to type something in but you have to abort it. in my case this was true to commandline commands for zeroconf (example for linuux is avahi)

if you want to run that on a button click, you of course cannot use system("yourCommand")
because then your button event will never end.
first idea would be now to create your own thread class via pthreads, and start the thread inside the button event, its execute routine calling this system("yourcommand").
unfortunately this will also cause wxWidgets to hang.
wxWidgets itself has an own command called wxSystem for doing system commands. running wxSystem("yourcommand") from your pthread class will also prove no success:
your wxWidgets window will freeze.
but you can do it with wxExecute()


i finally succeeded in executing the command with wxExecute with the wxAnythread flag or how it is called. this finally did the trick. and, oh yeah: do not try to run a wxExecute() in the OnExit() (?) method of your wxWidget app. It brought the app to crash under linux. but you can run it in the YourFrame:OnClose() method.

Comments

Anonymous said…
Thanks! Buddy i really need this...
Thanks a lot!
Schmendrick said…
you are welcome.
after all, i was publishing my experience to help others to get to a solution.

but comments are of course highly appreciated