如果电源故障/拔掉计算机,则注销用户,如何处理电源故障 [英] Unlogged out user if power failures/unplugging computer, how to handle power failures

查看:29
本文介绍了如果电源故障/拔掉计算机,则注销用户,如何处理电源故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我有一个表 UserAccounts 和列 LoginStatus如果用户登录用户 loginstatus = 1 否则如果他注销它是 = 0

First I have a table UserAccounts with Column LoginStatus if the user logged in the user loginstatus = 1 else if he logged out it is = 0

如果用户不小心关闭计算机或发生电源故障,您认为会处理什么代码.我需要更改 loginstatus = 0 否则如果发生这些事件它会自动注销.

what code do you think that will handle if the user accidentally plug off the computer or a power failure occurs. i need to change the loginstatus = 0 or it will auto logout if those events occurs.

顺便说一下,我正在使用 sql server

by the way im using sql server

我在关闭表单时使用了此代码,但如果我在应用程序运行时直接从开始菜单关闭计算机,它只会更改登录状态

i've used this code in form closing but it only change the loginstatus if i shutdown computer from start menu directly while the application is running

private void MPTestTablesFormClosing(object sender, FormClosingEventArgs e)
{
    if(e.CloseReason == CloseReason.WindowsShutDown)
    {
       var repo = new UserAccountsRepo();
       var backRepo = new UserLogRepo();

       var logs = new UserLogs
       {
          UserAccount = Globals.UserAccount,
          LogOutTime = DateTime.Now
       };

       Globals.UserAccount.LogStatus = 0;
       repo.Update(Globals.UserAccount);
       backRepo.Update(logs);
       Globals.UserAccount = null;
    }
}

推荐答案

一种可能的解决方案是跟踪您的应用程序没有完全关闭这一事实.也就是说,在持久性介质中的某个位置保存了一个标志,该标志在您的应用程序启动时设置,并在您的应用程序完全存在时重置.

One possible solution is to keep track of the fact that your application did not close cleanly. I..e have a flag saved somewhere in a persistent medium that gets set at start-up of your application, and that gets reset when your application exists cleanly.

然后在启动时检查这个标志.如果它仍然设置,您就知道您的应用程序没有干净地退出,您可以采取适当的(恢复)步骤回到稳定的未损坏状态.

Then at startup check this flag. If it is still set, you known that your application did not exit cleanly, and you can take appropriate (recovery) steps to get back into a stable non-corrupted situation.

这篇关于如果电源故障/拔掉计算机,则注销用户,如何处理电源故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆