系统要关闭,可我程序还有事要处理?

问题1描述:

  运行了一个客户端程序,服务端表明客户端在线,突然运行客户端程序的电脑关机了,但服务端并不知道客户端下线了,服务端的状态并不能及时更新为下线.

解决方法:

  通过截获系统的关闭消息,在系统关闭前对程序进行必要的处理.

  系统在关闭(关机/重启/注销)之前会向每个当前正在运行的应用程序发送WM_QUERYENDSESSION消息,询问系统能否关闭,系统再向应用程序发送WM_ENDSESSION消息,根据WM_QUERYENDSESSION消息的返回结果,从而通知应用程序是否已经结束.

  所以我们可以截获:WM_QUERYENDSESSION消息,在此消息中对程序进行必要的处理,处理成功后返回TRUE,表示此程序已经结束,系统可以关闭.

问题2描述:

  通过截获系统的关闭消息,在系统关闭前,可以成功对程序进行必要的处理.但是往往多个程序之间是有关联的,比如问题1的客户端程序系统关闭前会向服务端发送下线消息,但这时在对客户端程序进行关闭时客户端程序处理时,有可能电脑的网络已经断开,或者通过代理软件连网的软件已经断开,可想而知,客户端程序在断网前没有成功发送消息给服务端。

解决方法:

    在系统关闭期间,可以为指定的程序设置相对于其它程序的关闭顺序。

    SetProcessShutdownParmeters(Ox04FF,SHUNDOWN_NORETRY);


附MSDN消息解释:


WM_ENDSESSION


The WM_ENDSESSION message is sent to an application after the system processes the results of the WM_QUERYENDSESSION message. The WM_ENDSESSION message informs the application whether the session is ending.
A window receives this message through its WindowProc function.
LRESULT CALLBACK WindowProc(
  HWND hwnd,       // handle to window
  UINT uMsg,       // message identifier
  WPARAM wParam,   // end-session option
  LPARAM lParam    // logoff option
);
Parameters
hwnd
Handle to window.
uMsg
WM_ENDSESSION identifier.
wParam
If the session is being ended, this parameter is TRUE; otherwise, it is FALSE. (表示是否要终止程序,如果是TRUE,指令该程序终止,否则是FALSE)
lParam
If this parameter includes ENDSESSION_LOGOFF, the user is logging off; otherwise, the user is shutting down the system. (Note that this parameter is a bit mask. To test for this value, use a bit-wise operation; do not test for equality.) (表示用户注销还是系统被关闭。如果此参数包含ENDSESSION_LOGOFF,则表示系统注销。)
If this parameter is zero, the system is shutting down.
Return Values
If an application processes this message, it should return zero.

WM_QUERYENDSESSION


The WM_QUERYENDSESSION message is sent when the user chooses to end the session or when an application calls the ExitWindows function. If any application returns zero, the session is not ended. The system stops sending WM_QUERYENDSESSION messages as soon as one application returns zero.
After processing this message, the system sends the WM_ENDSESSION message with the wParam parameter set to the results of the WM_QUERYENDSESSION message.
A window receives this message through its WindowProc function.
LRESULT CALLBACK WindowProc(
  HWND hwnd,       // handle to window
  UINT uMsg,       // message identifier
  WPARAM wParam,   // not used
  LPARAM lParam    // logoff option
);
Parameters
hwnd
Handle to window.
uMsg
WM_QUERYENDSESSION identifier.
wParam
This parameter is reserved for future use.
lParam
If this parameter includes ENDSESSION_LOGOFF, the user is logging off. (Note that this parameter is a bit mask. To test for this value, use a bit-wise operation; do not test for equality.)
If this parameter is zero, the system is shutting down or restarting (it is not possible to determine which event is occurring).
Return Values
If an application can terminate conveniently, it should return TRUE; otherwise, it should return FALSE.

SetProcessShutdownParameters

The SetProcessShutdownParameters function sets shutdown parameters for the currently calling process. This function sets a shutdown order for a process relative to the other processes in the system.
BOOL SetProcessShutdownParameters(
  DWORD dwLevel,
  DWORD dwFlags
);
Parameters
dwLevel
[in] Shutdown priority for a process relative to other processes in the system. The system shuts down processes from high dwLevel values to low. The highest and lowest shutdown priorities are reserved for system components. This parameter must be in the following range of values. Value Meaning
000-0FF System reserved last shutdown range.
100-1FF Application reserved last shutdown range.
200-2FF Application reserved "in between" shutdown range.
300-3FF Application reserved first shutdown range.
400-4FF System reserved first shutdown range.
All processes start at shutdown level 0x280.
dwFlags
[in] Flags. This parameter can be the following value. Value Meaning
SHUTDOWN_NORETRY
0x00000001 The system terminates the process without displaying a retry dialog box for the user.
Return Values
If the function is succeeds, the return value is nonzero.


 

原文地址:https://www.cnblogs.com/joinclear/p/1890539.html