Page 1 of 1

How do I Terminate a 'stand alone' program ?

Posted: Fri May 22, 2015 1:19 am
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

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

Posted: Fri May 22, 2015 2:16 am
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

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

Posted: Fri May 22, 2015 6:44 am
by netmaestro

Code: Select all

SendMessage_(hwnd, #WM_SYSCOMMAND, #SC_CLOSE, 0)

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

Posted: Fri May 22, 2015 4:48 pm
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

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

Posted: Fri May 22, 2015 5:42 pm
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:

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

Posted: Sat May 23, 2015 3:46 pm
by vmars316
Thanks TI-994A , great info ... Thanks...Vern