Author Topic: Terminate an Executing Program using another QB64 program  (Read 3581 times)

0 Members and 1 Guest are viewing this topic.

Offline MLambert

  • Forum Regular
  • Posts: 115
    • View Profile
Terminate an Executing Program using another QB64 program
« on: May 20, 2019, 07:01:49 am »
Hi,

How do I terminate a program that is currently running using another QB64 program ?

Mike

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
« Last Edit: May 20, 2019, 07:21:20 am by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline MLambert

  • Forum Regular
  • Posts: 115
    • View Profile
Re: Terminate an Executing Program using another QB64 program
« Reply #2 on: May 20, 2019, 07:08:11 pm »
How do I obtain the PID ?

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Terminate an Executing Program using another QB64 program
« Reply #3 on: May 20, 2019, 09:02:15 pm »
If you are using Windows, just look up the name of the app in with Windows Task. Find it in the "Process" tab and highlight it. Next, right click, and select "Go to details" In details, you will see the name of the file, and the next column shows the PID.

Example for Paint.net program...

SHELL _HIDE _DONTWAIT "taskkill /F /IM paintdotnet.exe"

or, by process ID,

SHELL _HIDE _DONTWAIT "taskkill /F /PID 10328"

Now the process ID is something that changes each time a program runs, so if you want to go that route, you will need a remote lookup method. I figured out how to get that yesterday, but only for the QB64 app. Anyway, I'll combine that lookup procedure with taskkill in this next example...

Code: QB64: [Select]
  1.     FUNCTION GetCurrentProcessId& ()
  2.  
  3. dwProcessId& = GetCurrentProcessId&
  4.  
  5. PRINT dwProcessId&
  6. PRINT "Press any key to shut this program down...
  7. SLEEP
  8. SHELL _HIDE _DONTWAIT "taskkill /F /PID "+ LTRIM$(STR$(dwProcessId&))
  9.  
  10.  

So maybe you could do some research and find a way to get the process ID of the program you want, but I have to warn you, API stuff is a PITA.

EDIT: Oh, I almost forgot about tasklist...

SHELL _DONTWAIT "tasklist>tmp.tmp"
_DELAY 1: SHELL _DONTWAIT "notepad tmp.tmp"

That will make a file tmp.tmp in your local QB64 directory that the code will open in Notepad. It will show you a list of all handle image names and process IDs, etc. YOu can taylor the output with switches, as seen in this resource: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/tasklist

You might be able to incorporate that in your program to find and shutdown a particular process.

Pete
« Last Edit: May 20, 2019, 09:26:18 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline MLambert

  • Forum Regular
  • Posts: 115
    • View Profile
Re: Terminate an Executing Program using another QB64 program
« Reply #4 on: May 21, 2019, 06:33:18 am »
Thank you ..
« Last Edit: May 21, 2019, 06:38:46 am by MLambert »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Terminate an Executing Program using another QB64 program
« Reply #5 on: May 21, 2019, 09:27:03 am »
Depending on the operation, terminating by PID can be a very complex issue. For instance, let's say I have three of the same paint.net programs running. They all have the same image name, but different PIDs. I only want to terminate the second one. So, how do I do that? Well, I can't use the image name, because it would close all three, so hey, I'll terminate by PID, instead! Well, which PID? Why, the second one, of course! Ah, but how do I determine which one is the second one? Well, if task list creates a time ordered list, that would not be a problem, but if it doesn't, things get pretty open for bad results, even with additional programming of a QB64 app running in the background to try to add a time stamp to that task list. So, I'd have to test out has task list orders the list of processes running, and hope it is by time. If not, more work to do.

So, I'm not sure what you are trying to achieve here, simple termination by image name, or you need the PID for a more complex issue, as discussed above. There may be alternative solutions, too. I just don't know of any at the moment. At any rate, no, this cannot be done natively in QB64, much like keeping a window persistent can't be done natively, either. Frankly, if it wasn't for SHELL, I would have pitched QBasic back in the 1990's. It at least made it possible to achieve some of the things that were impossible in native QB. Unfortunately, it was just too daunting a task for Rob to make all those SHELL functions, Windows API functions, which can be done with libraries, native to QB64. I'm just assuming he could have, if he had the time, help, and energy.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Raven_Singularity

  • Forum Regular
  • Posts: 158
    • View Profile
Re: Terminate an Executing Program using another QB64 program
« Reply #6 on: May 23, 2019, 03:01:38 pm »
If they're your apps, you could set the title to something unique.

Offline MLambert

  • Forum Regular
  • Posts: 115
    • View Profile
Re: Terminate an Executing Program using another QB64 program
« Reply #7 on: May 26, 2019, 02:57:12 am »
Thank you everyone for the great help.

Mike