How do I Terminate a 'stand alone' program ?

Just starting out? Need help? Post your questions and find answers here.
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

How do I Terminate a 'stand alone' program ?

Post by vmars316 »

Hello & Thanks ,
I see this example program :

Code: Select all

; Start a process and later terminate it yourself...
; Prozess beenden: 
Debug "Start program..."
processID.l = RunProgram("notepad.exe","","",#PB_Program_Open) 
Debug "ProcessID = " + Str(processID)

Debug "We will wait 3 seconds now..."
Delay(1000)
Debug "We will wait 2 seconds now..."
Delay(1000)
Debug "We will wait 1 seconds now..."
Delay(1000)

KillProgram(processID)
CloseProgram(processID)

Debug "Program terminated..."
But 'in general' how do I Terminate a 'stand alone' program
that I started by double-clicking an *.exe , or from compiler (F5) ,
other than clicking on 'Red X' button?
I also tried 'CloseProgram(self)

Thanks
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
dchisholm
New User
New User
Posts: 7
Joined: Tue Mar 31, 2015 9:44 pm
Location: Ottawa, ON Canada
Contact:

Re: How do I Terminate a 'stand alone' program ?

Post by dchisholm »

As long as your program is a single process with no threads ou could use the End command.

Here is a simple example:

test.pbf

Code: Select all

Global Window_0

Enumeration FormGadget
  #Text_0
EndEnumeration


Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu)
  TextGadget(#Text_0, 160, 170, 250, 25, "THIS IS MY TEST FORM", #PB_Text_Center)
EndProcedure

Procedure Window_0_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False

    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure
test.pb

Code: Select all

IncludeFile "test.pbf"

Define.l event

OpenWindow_0()

While (Window_0_Events(event))
  
  Delay(2000)
  
  End 
  
Wend
This will open a window and after two seconds it will shut down unless you click the "x" before two seconds.

/Dave
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: How do I Terminate a 'stand alone' program ?

Post by netmaestro »

Code: Select all

SendMessage_(hwnd, #WM_SYSCOMMAND, #SC_CLOSE, 0)
BERESHEIT
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: How do I Terminate a 'stand alone' program ?

Post by vmars316 »

Thanks All ,

END
Works fine .

Procedure ExitBtn_clicked(EventType)
MessageRequester("ExitBtn_Clicked:", "ExitBtn_Clicked")
; CloseProgram(self)
SendMessage_(hwnd, #WM_SYSCOMMAND, #SC_CLOSE, 0)
MessageRequester("ExitBtn_Clicked:", "SendMessage")
End
EndProcedure

SendMessage doesn't work as coded above.
Do I need to load "hwnd" with something ?

Thanks...Vern
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
User avatar
TI-994A
Addict
Addict
Posts: 2705
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: How do I Terminate a 'stand alone' program ?

Post by TI-994A »

vmars316 wrote:...how do I Terminate a 'stand alone' program that I started by double-clicking an *.exe , or from compiler (F5) , other than clicking on 'Red X' button?
Hi Vern. I think some clarification is in order.

Pressing F5 in the IDE simply executes the currently loaded PureBasic program. In that case, to terminate such a program, other than clicking on 'Red X' button, simply implement a flag that breaks the event loop, like this:

Code: Select all

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, #PB_Any, #PB_Any, 600, 400, "Main Window", wFlags)
ButtonGadget(1, 250, 180, 100, 30, "E X I T")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          appQuit = 1
      EndSelect
  EndSelect
Until appQuit = 1
Alternatively, the PostEvent() function could also be used:

Code: Select all

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, #PB_Any, #PB_Any, 600, 400, "Main Window", wFlags)
ButtonGadget(1, 250, 180, 100, 30, "E X I T")

Repeat
  event = WaitWindowEvent()
  Select event
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          PostEvent(#PB_Event_CloseWindow)
      EndSelect
  EndSelect
Until event = #PB_Event_CloseWindow
If I've understood correctly. :wink:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: How do I Terminate a 'stand alone' program ?

Post by vmars316 »

Thanks TI-994A , great info ... Thanks...Vern
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
Post Reply