# Kill program being executed in Xojo Shell

**URL:** <https://ifnotnil.com/t/kill-program-being-executed-in-xojo-shell/923>\
**Category:** General Programming\
**Created:** [6 September 2020 02:05 UTC](https://ifnotnil.com/t/kill-program-being-executed-in-xojo-shell/923 "2020-09-06T02:05:01Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Meestor\_X](https://ifnotnil.com/user_avatar/ifnotnil.com/meestor_x/32/567_2.png) [@Meestor\_X](https://ifnotnil.com/u/Meestor_X)\
**Post date:** [6 September 2020 02:05 UTC](https://ifnotnil.com/t/kill-program-being-executed-in-xojo-shell/923/1 "2020-09-06T02:05:01Z")

</div>

Shell.Close kills the application that’s been executed in a shell on MacOS, but not in Windows.

`Shell.Write(ChrB(3))` and `Shell.Execute(ChrB(3))` doesn’t do it either. (Canonical On)

Anyone been able to fuck that pig? (LetterKenny reference)

---

<div class="post-metadata">

**Author:** ![samRowlands](https://ifnotnil.com/user_avatar/ifnotnil.com/samrowlands/32/121_2.png) [@samRowlands](https://ifnotnil.com/u/samRowlands)\
**Post date:** [6 September 2020 02:17 UTC](https://ifnotnil.com/t/kill-program-being-executed-in-xojo-shell/923/2 "2020-09-06T02:17:31Z")

</div>

Can you get the PID of the process and kill it that way?

---

<div class="post-metadata">

**Author:** ![PatrickSalo](https://ifnotnil.com/user_avatar/ifnotnil.com/patricksalo/32/670_2.png) [@PatrickSalo](https://ifnotnil.com/u/PatrickSalo)\
**Post date:** [6 September 2020 02:26 UTC](https://ifnotnil.com/t/kill-program-being-executed-in-xojo-shell/923/3 "2020-09-06T02:26:13Z")

</div>

I haven’t run across this one before and theoretically .close should have done it.

Can you force your shell to go out of scope in Xojo?

Heck, I don’t even know if this would work, but I guess you could try sending over an “exit” to the shell before doing a .close to see if that does anything.

---

<div class="post-metadata">

**Author:** ![Meestor\_X](https://ifnotnil.com/user_avatar/ifnotnil.com/meestor_x/32/567_2.png) [@Meestor\_X](https://ifnotnil.com/u/Meestor_X)\
**Post date:** [6 September 2020 02:27 UTC](https://ifnotnil.com/t/kill-program-being-executed-in-xojo-shell/923/4 "2020-09-06T02:27:21Z")

</div>

Shell.PID doesn’t match the app.

Lol.

---

<div class="post-metadata">

**Author:** ![Meestor\_X](https://ifnotnil.com/user_avatar/ifnotnil.com/meestor_x/32/567_2.png) [@Meestor\_X](https://ifnotnil.com/u/Meestor_X)\
**Post date:** [6 September 2020 02:29 UTC](https://ifnotnil.com/t/kill-program-being-executed-in-xojo-shell/923/5 "2020-09-06T02:29:14Z")

</div>

The normal way of closing this program when running in a command (terminal) window in Windows is Pressing Ctrl-C. (Cmd-C works in MacOS). Works fine when I run it manually, but not from a Shell in Xojo.

---

<div class="post-metadata">

**Author:** ![Meestor\_X](https://ifnotnil.com/user_avatar/ifnotnil.com/meestor_x/32/567_2.png) [@Meestor\_X](https://ifnotnil.com/u/Meestor_X)\
**Post date:** [6 September 2020 02:39 UTC](https://ifnotnil.com/t/kill-program-being-executed-in-xojo-shell/923/6 "2020-09-06T02:39:26Z")

</div>

> [@PatrickSalo](#):
>
> I haven’t run across this one before and theoretically .close should have done it.

Lol.

Getting right on top of those bugs, I see…

---

<div class="post-metadata">

**Author:** ![PatrickSalo](https://ifnotnil.com/user_avatar/ifnotnil.com/patricksalo/32/670_2.png) [@PatrickSalo](https://ifnotnil.com/u/PatrickSalo)\
**Post date:** [6 September 2020 03:40 UTC](https://ifnotnil.com/t/kill-program-being-executed-in-xojo-shell/923/7 "2020-09-06T03:40:07Z")

</div>

It’s been ages since I’ve done Xojo shell work in Windows, but last time I remember I thought all the processes run under whatever shell object you have in Xojo. So if you tear down that object, then all children should be terminated as well. Of course this is for things not told to go into the background (e.g. & over here in Mac bash-land).

What mode of shell are you working under?

---

<div class="post-metadata">

**Author:** ![Meestor\_X](https://ifnotnil.com/user_avatar/ifnotnil.com/meestor_x/32/567_2.png) [@Meestor\_X](https://ifnotnil.com/u/Meestor_X)\
**Post date:** [6 September 2020 07:23 UTC](https://ifnotnil.com/t/kill-program-being-executed-in-xojo-shell/923/8 "2020-09-06T07:23:01Z")

</div>

Mode 2.

Plenty of threads about this on the “other” forum, and I linked up a couple of posts a couple of bug reports that were “verified” years ago, but never fixed.  
From the “other” forum:

> **[Closing Shell app under windows](https://forum.xojo.com/t/closing-shell-app-under-windows/11291)**
>
> I execute a console application from my main application using a shell execution and all works fine. But when I close the main application, the console app keeps running and won’t be killed. Not even when I close the shell manually with shell.close....

  

> **[Force-quit a Shell (Windows)](https://forum.xojo.com/t/force-quit-a-shell-windows/12099)**
>
> Does anyone know how to force quit a Shell in Windows? Calling the Shell.Close method does not seem to accomplish this. RealStudio 2012 R1.1

  

> **[orphaned command line process in Windows 7/RB2011](https://forum.xojo.com/t/orphaned-command-line-process-in-windows-7-rb2011/17778)**
>
> First, I’m using RealStudio 2011 R1.1. I’m building a front end to the x264 encoder, which is command-line based. My application works pretty well so far, but I’m unable to cancel the process that’s spawned by my shell object, and with x264 it’s...

  
(the methods suggested, other than `TaskKill` do not work, btw)

So, the shell doesn’t work as expected, that’s for sure. Likely the easiest workaround will be a  
`Taskkill /IM myprogram.exe /F`  
from another shell.

---

<div class="post-metadata">

**Author:** ![MonkeybreadSoftware](https://ifnotnil.com/user_avatar/ifnotnil.com/monkeybreadsoftware/32/451_2.png) [@MonkeybreadSoftware](https://ifnotnil.com/u/MonkeybreadSoftware)\
**Post date:** [6 September 2020 07:56 UTC](https://ifnotnil.com/t/kill-program-being-executed-in-xojo-shell/923/9 "2020-09-06T07:56:08Z")

</div>

You can always try [ShellMBS](https://www.monkeybreadsoftware.net/class-shellmbs.shtml) or [WindowsProcessMBS](https://www.monkeybreadsoftware.net/class-windowsprocessmbs.shtml) classes in [MBS Xojo Plugins](https://www.monkeybreadsoftware.de/xojo/) if you need a different class.  
(And we created those to handle some things better)

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [6 September 2020 17:10 UTC](https://ifnotnil.com/t/kill-program-being-executed-in-xojo-shell/923/10 "2020-09-06T17:10:50Z")

</div>

When you start a process using the shell it does NOT start it as a dependent child process that will die when the parent dies  
You can / could use a declare to ShellExecuteW to make sure this happens  
Or the MBS plugins as well

There are ways to make it work

---

<div class="post-metadata">

**Author:** ![Meestor\_X](https://ifnotnil.com/user_avatar/ifnotnil.com/meestor_x/32/567_2.png) [@Meestor\_X](https://ifnotnil.com/u/Meestor_X)\
**Post date:** [6 September 2020 17:28 UTC](https://ifnotnil.com/t/kill-program-being-executed-in-xojo-shell/923/11 "2020-09-06T17:28:24Z")

</div>

> [@npalardy](#):
>
> When you start a process using the shell it does NOT start it as a dependent child process that will die when the parent dies

Except that it does die on MacOS, so I was expecting the same behaviour on Windows. (Oh, fool me!)

> [@npalardy](#):
>
> There are ways to make it work

Indeed. Using TaskKill on Windows works fine. I’ll have to see what happens on Linux when I get to that bridge.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [6 September 2020 17:32 UTC](https://ifnotnil.com/t/kill-program-being-executed-in-xojo-shell/923/12 "2020-09-06T17:32:45Z")

</div>

I’d expect linux to behave more like macOS  
It has to do with how the OS actually starts processes  
On Windows its not the default to start them as child processes that are configured to die when the parent dies. But, if you use the Windows API to start a process its an option.  
I dont think the Shell use that API configured that way to run your process - hence the weird behaviour

You could subclass shell and add a a Close method or in the Destructor of your subclass simply issue the task kill and then use your subclass everywhere and its then invisible to you there is a difference between platforms 🙂

---

<div class="post-metadata">

**Author:** ![Meestor\_X](https://ifnotnil.com/user_avatar/ifnotnil.com/meestor_x/32/567_2.png) [@Meestor\_X](https://ifnotnil.com/u/Meestor_X)\
**Post date:** [6 September 2020 17:49 UTC](https://ifnotnil.com/t/kill-program-being-executed-in-xojo-shell/923/13 "2020-09-06T17:49:49Z")

</div>

> [@npalardy](#):
>
> You could subclass shell and add a a Close method or in the Destructor of your subclass simply issue the task kill and then use your subclass everywhere and its then invisible to you there is a difference between platforms 🙂

Great minds and all that…

```auto
Class KillableShell
Inherits Shell
  
  Sub Kill(App As String)

    #If TargetWindows
      Dim sh As New Shell
      sh.mode = 0 ' One-shot 
      sh.Execute("TaskKill /IM " + App + " /F") 
      System.DebugLog(sh.Result)
    #EndIf

    Me.Close 
  End Sub
End Class

```
