react on rundown?

Windows specific forum
Spock
New User
New User
Posts: 3
Joined: Thu Aug 07, 2003 1:52 pm

react on rundown?

Post by Spock »

hallo,

is it possible to dedect a "end task" signal in pb?So I can do some imprtend stuff? Like, for example, the notepad ask to save unsaved text when I do an "End Task" in the Taskmanager.
Fred
Administrator
Administrator
Posts: 18199
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

look at WM_QUERYENDSESSION and WM_ENDSESSION messages in the API, it should be what you are looking for.
Spock
New User
New User
Posts: 3
Joined: Thu Aug 07, 2003 1:52 pm

Post by Spock »

sounds ot bad, but i forget to importend things:

- i'm a beginner, can you explain it a bit or have you an example? Have I to poll this msg?


- dose it work on WindowApps only or also on an Console App, beqause I try to write a simpel Console based programm only.

thanks,
Spock
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: react on rundown?

Post by PB »

> is it possible to dedect a "end task" signal in pb?

I don't think so. By design, ending a task with the Task Manager means
the task doesn't know it's going to happen. Fred's reply about the two
EndSession flags are only for when Windows itself is exiting.
Spock
New User
New User
Posts: 3
Joined: Thu Aug 07, 2003 1:52 pm

Post by Spock »

>I don't think so. By design, ending a task with the Task Manager means
>the task doesn't know it's going to happen. Fred's reply about the two
>EndSession flags are only for when Windows itself is exiting.

I think you are talkng about "end process" thats different from "end task", you can try it with notepad, "end process" kills notepad, and any unsaved data are lost, but with a "end task" notepad will ask you if you want to save the data before stopping.

Spock!
Proteus
Enthusiast
Enthusiast
Posts: 113
Joined: Wed Sep 17, 2003 8:04 pm
Location: The Netherlands

Post by Proteus »

Spock, just put your code after the event loop and before the end statement.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> I think you are talkng about "end process" thats different from "end task"

Actually, it all depends on which version of Windows you're using.

Windows 9x uses an "End Task" button (in CTRL+ALT+DEL) to kill a process,
but Windows 2K/XP uses "End Process" as the button name. Both buttons
will kill the app without the app knowing it's going to happen.
SeregaZ
Enthusiast
Enthusiast
Posts: 628
Joined: Fri Feb 20, 2009 9:24 am
Location: Almaty (Kazakhstan. not Borat, but Triple G)
Contact:

Re: react on rundown?

Post by SeregaZ »

after 7 years i am still know how to made it :)

i have 2 part of code:

Code: Select all

Declare.l MyWindowCallback(WindowID, Message, wParam, lParam) 

OpenWindow(0,0,0,320,200, "Test Window",#PB_Window_SystemMenu) 

SetWindowCallback(@MyWindowCallback()) 

Procedure.l MyWindowCallback(WindowID, Message, wParam, lParam) 

  Result = #PB_ProcessPureBasicEvents 
  
  If Message = $16 ; End Session Message 
      If wParam 
         ; the section is ending 
         If lParam 
            ;user is logging off 
            MessageRequester("X", "User is is logging off.") 
            Else 
            ;user is shutting down the system. 
            MessageRequester("X", "User is shutting down the system.") 
         EndIf 
      EndIf 
  EndIf 
  
  ProcedureReturn Result 
  
EndProcedure 

Repeat 
  If WaitWindowEvent() = #PB_Event_CloseWindow: Break: EndIf 
ForEver 
and

Code: Select all

Procedure WindowsCallback(WindowID,Message,wParam,lParam) 
  If Message=#WM_QUERYENDSESSION 
    ProcedureReturn 0 ; Aborts shutdown. 
  Else 
    ProcedureReturn #PB_ProcessPureBasicEvents 
  EndIf 
EndProcedure 

OpenWindow(0,200,200,300,100,"Try to shutdown!",#PB_Window_SystemMenu) 
SetWindowCallback(@WindowsCallback()) 

Repeat : 
Until WaitWindowEvent()=#PB_Event_CloseWindow
the second example is stop the windows shutdown. how i can make like this, but not to stop - only pause. when my programm is made some before quit code, only after this shutdown (or restart, or logoff) is remain.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: react on rundown?

Post by netmaestro »

The OS is going to wait until it gets a response from all the running processes before shutting down. Any process that returns #Null from WM_QUERYENDSESSION will stop the shutdown, but you don't want to return #Null, you just want to take some action and then, when you're ready, return #True:

Code: Select all

Procedure WindowsCallback(WindowID,Message,wParam,lParam) 
  If Message=#WM_QUERYENDSESSION
    result = MessageRequester("Note:", "Save <project name here> before windows shutdown?", #PB_MessageRequester_YesNoCancel)
    If result = #PB_MessageRequester_Yes
      ; simulate a save
      If CreateFile(0, GetTemporaryDirectory()+"test.txt")
        WriteString(0, "Saved stuff!")
        CloseFile(0)
      EndIf
    EndIf
    ProcedureReturn #True 
  Else 
    ProcedureReturn #PB_ProcessPureBasicEvents 
  EndIf 
EndProcedure 

OpenWindow(0,200,200,300,100,"Try to shutdown!",#PB_Window_SystemMenu) 
SetWindowCallback(@WindowsCallback()) 

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
BERESHEIT
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: react on rundown?

Post by PB »

> The OS is going to wait until it gets a response from all the running processes before shutting down

Not quite. It does shut down after a specific time (default is 20 seconds).
There are many web sites that explain how to change that timeout in the
Registry to force a fast shutdown without letting apps save their data.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
SeregaZ
Enthusiast
Enthusiast
Posts: 628
Joined: Fri Feb 20, 2009 9:24 am
Location: Almaty (Kazakhstan. not Borat, but Triple G)
Contact:

Re: react on rundown?

Post by SeregaZ »

didnt work :) this dialog Y/N is shows, but windows shutdown is continued, and after a few seconds program still terminated uncorrectly.
for test - try to restart or shutdown windows xp when in microsoft office word document present not saved data - he will waite as long, as you need to press button.
i think must be some pause win api command... the truth is out there.


maybe like this will work:
1. detecting what exactly happening restart, shutdown or logoff
2. full stop shutdown process
3. run some "prepeare for end" code
4. start windows restart, shutdown or logoff - that we detect in "1" - by my programm herself
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: react on rundown?

Post by doctorized »

PB is right, there is no 'End Proccess' event.

If we need to be a little more accurate, but not 100%:

Code: Select all

Declare.l MyWindowCallback(WindowID, Message, wParam, lParam)

OpenWindow(0,0,0,320,200, "Test Window",#PB_Window_SystemMenu)

SetWindowCallback(@MyWindowCallback())

Procedure.l MyWindowCallback(WindowID, Message, wParam, lParam)

  Result = #PB_ProcessPureBasicEvents
  If Message = $10; we closed the program from the 'Programs' tab.
      MessageRequester("X","we closed the program from the 'Programs' tab.")
  ElseIf Message = $112; we closed the program from the windows bar
      MessageRequester("ok","we closed the program from the windows bar")
  ElseIf Message = $16 ; End Session Message
      MessageRequester("ok","$16")
      If wParam
         ; the section is ending
         If lParam
            ;user is logging off
            MessageRequester("X", "User is is logging off.")
            Else
            ;user is shutting down the system.
            MessageRequester("X", "User is shutting down the system.")
         EndIf
      EndIf
  EndIf
 
  ProcedureReturn Result
 
EndProcedure

Repeat
  If WaitWindowEvent() = #PB_Event_CloseWindow: Break: EndIf
ForEver 
; after message $112 these messages are following: $90, 2, $82.
Post Reply