简体   繁体   English

如果应用程序在c#winforms中意外关闭,则更新数据库

[英]Update Database if Application Close unexpectedly in c# winforms

I am developing desktop based winform in c#. 我正在用C#开发基于桌面的Winform。 The application may closed unexpectedly (power failure or debug mode off). 应用程序可能会意外关闭(电源故障或调试模式关闭)。 In any closing condition i need to update my database . 在任何关闭条件下,我都需要更新数据库。 ie I need to change the online status from 1 to 0 how to do this? 即我需要将在线状态从1更改为0,该怎么做?

The best way to know whether an application has died is not to try to think of a way to update the database when the application closes; 知道应用程序是否已死亡的最好方法是不要尝试考虑在应用程序关闭时更新数据库的方法。 but rather, you could create a "session" record whenever the application is opened; 但是,只要打开应用程序,您就可以创建一个“会话”记录; then "ping" the record (ie there's a LastPingUTC column) every minute. 然后每分钟“ ping”记录(即有一个LastPingUTC列)。

If the application closes for whatever reason, the LastPingUTC value will be over 1 minute ago and therefore you can assume the application / session is dead. 如果由于某种原因关闭了应用程序,则LastPingUTC值将超过1分钟,因此您可以假定应用程序/会话已死。

Actually, I'd assume a given session is dead after 5 minutes as updating a column may be delayed. 实际上,我假设给定的会话在5分钟后就死了,因为更新列可能会延迟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM