系统要关闭,可我程序还有事要处理?
系統要關閉,可我程序還有事要處理?
問題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/Dageking/archive/2013/03/24/2979314.html
總結
以上是生活随笔為你收集整理的系统要关闭,可我程序还有事要处理?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用jsp+swfupload做上传时报
- 下一篇: Linux下Json库的编译及代码测试