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

问题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

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

  1. Win10系统如何关闭开机自启动程序,看这里就够了,怎样关闭Windows10电脑自启动软件

    现如今我们大家不论是生活中还是工作中运用电脑都是甚为广泛,对于电脑的操作,有人会把一些软件设置为开机自启动,如果当我们不需要自启动的时候怎样关闭自启动程序呢?不能自启动软件开一次你手动关一次吧,这样太 ...

  2. 好系统重装助手分享win10系统强制关闭未响应程序的方法

    我们在使用电脑的时候,总会遇到程序一直显示未响应.卡顿崩溃的情况,等着浪费时间,却又不知道怎么办.下面就由好系统重装助手来为大家讲解一下如何强制关闭未响应程序吧.具体操作步骤如下: 1.快捷组合键:C ...

  3. linux系统如何关闭程序,关闭和重启Linux系统

    关闭和重启系统 Linux系统下常用的关闭和重启系统的命令有"shutdown"."halt"."reboot"和"init&qu ...

  4. Win10电脑重装系统更新关闭了还自动打开怎么解决?

    ​Win10更新关闭了还自动打开?由于电脑系统更新有时候会遇到一些问题,例如会带来一些新的BUG,这使得有用户就会关闭系统更新,但是近期有用户反映更新服务禁止了还更新的情况,那么Win10电脑重装系统 ...

  5. 关闭计算机启动程序,电脑开机时自启动程序烦人,教你用系统自带程序关闭它!...

    电脑每次开启的时候会有一些应用程序也跟着自动启动,占用着电脑系统资源.有时候我们并不需要这些程序开启,那么我们如何关闭这些的开机自动启动项呢?这时,有装一些第三方安全管理软件的可以利用它们来管理,但这 ...

  6. xp关无线配置服务器,xp系统怎么关闭无线功能

    xp系统怎么关闭无线功能 现在科学技术发展非常快,很多新的技术都层出不穷,新的无线技术就很让人着迷,使用起来也是有一定的方便之处,只是现在看来,发展还不是很成熟,有很多时候显得比较的鸡肋,而且电脑上的 ...

  7. xp系统开机必须启动的服务器,XP系统如何关闭开机启动项呢?

    如何快速开启电脑,关闭不必要的选项,有助于提高系统的开机速度.那么XP系统如何关闭开机启动项呢?其实它的操作方法有很多种,相信很多用户还不了解吧,下面小编为大家介绍一下XP系统关闭开机启动项的操作方法 ...

  8. 怎么关闭win10虚拟机服务器,win10系统彻底关闭退出vmware虚拟机的步骤

    有关win10系统彻底关闭退出vmware虚拟机的操作方法想必大家有所耳闻.但是能够对win10系统彻底关闭退出vmware虚拟机进行实际操作的人却不多.其实解决win10系统彻底关闭退出vmware ...

  9. 怎么关闭win10虚拟机服务器,大神为你win10系统彻底关闭退出vmware虚拟机的方案...

    大神为你win10系统彻底关闭退出vmware虚拟机的方案? 很多朋友安装win10系统后,在使用的过程中会遇到对win10系统彻底关闭退出vmware虚拟机进行设置的情况,可能有很多用户还是不能自己 ...

最新文章

  1. 在AcGIS随着大数据的生成DEM
  2. 200余行代码,让你实时从视频中隐身
  3. BZOJ 1711: [Usaco2007 Open]Dining吃饭
  4. eclipse打开,莫名报一堆错(Syntax error on tokens, delete these tokens )
  5. k means sse python_python实现kMeans算法
  6. python全栈学习--day13(迭代器,生成器)
  7. timimg学习数据删了_如何评价Timing这个督促人学习的软件?
  8. 计算机excel素材,excel操作练习题素材.doc
  9. linux自动内存清理
  10. 易语言高级表格写入MYSQL_易语言高级表格读写EXCEL源码
  11. PostgreSQL遍历Json
  12. 排序算法问题:稳定排序与不稳定排序
  13. 如何下载fatjar
  14. PCIe学习(一):PCIe基础及生成PIO例程分析
  15. 迪士尼无限3.0连接服务器,迪士尼无限3period;0BUG问题解决 | 手游网游页游攻略大全...
  16. vue3+tsx 踩坑
  17. 最新图文识别技术综述
  18. 关于华为ensp一些报错处理分享及基础命令
  19. 云游戏的2022:破局、新生、元宇宙
  20. Linux常用命令摘抄

热门文章

  1. VS2010 定位文件在solution中的位置
  2. 假如我是陈一舟--我的大社交生态系统战略
  3. Ilist与List的区别
  4. mysql 主键 uniqo_项目总结,彻底掌握NodeJS中如何使用Sequelize
  5. android ndk mac安装成功,Mac下Android Studio NDK 环境搭建并运行
  6. 快捷启动_start for mac(快捷启动应用)
  7. ExecuteScalar ExecuteReader ExecuteNonQuery 区别
  8. Android 仿微信小视频录制
  9. SparkSQL(Spark-1.4.0)实战系列(一)——DataFrames基础
  10. DatagridView内容自动换行和换行符换行