2

我正在开发一个应用程序以在后台运行以捕获用户在其系统上的活动,例如注销/关机/空闲/切换用户/继续按下任何键/系统锁定等。

它工作正常,我能够跟踪所有活动,现在我需要在系统锁定 15 分钟后自动注销用户。

我已经尝试了下面的代码。该ExitWindowsEx()功能在用户登录时工作正常,但在用户锁定系统后无法正常工作。

使用代码

[DllImport("user32")]
public static extern bool ExitWindowsEx(uint uFlags, uint dwReason);

private SessionSwitchEventHandler sseh;

void SysEventsCheck(object sender, SessionSwitchEventArgs e)
{
    switch (e.Reason)
    {
        case SessionSwitchReason.SessionLock:
           if(condition)
           {
               ExitWindowsEx(0, 0);
           }
           break;
    }    
}

任何人都可以帮助我在用户处于锁定状态时如何注销用户。

4

1 回答 1

0

终于找到了解决问题的方法,

代码

public static bool _IsLocked;
private SessionSwitchEventHandler sseh;

void SysEventsCheck(object sender, SessionSwitchEventArgs e)
{
    switch (e.Reason)
    {
        case SessionSwitchReason.SessionLock:
            if (!_IsLocked)
            {
                Process.Start("shutdown", "/r /f /t 900");
            }
            _IsLocked = true;
            break;
        case SessionSwitchReason.SessionUnlock:
            if (_IsLocked)
            {
                Process.Start("shutdown", "-a");
            }
            _IsLocked = false;
            break;
    }
}

以上代码将在系统锁定时安排系统重启(15 分钟),如果用户在 15 分钟之前解锁系统,代码将取消该计划,否则将在 15 分钟后重新启动系统。

重启调度程序代码

Process.Start("shutdown", "/r /f /t 900");

取消重启

Process.Start("shutdown", "-a");
于 2016-07-19T11:59:47.943 回答