以编程方式确定锁定工作站的持续时间?

以编程方式确定锁定工作站的持续时间?

如何在代码中确定机器锁定的时间?

C#之外的其他想法也是受欢迎的。


我喜欢Windows服务理念(并已接受它)以简化和清洁,但遗憾的是我不认为它在这种特殊情况下对我有用。我想在我的工作站上运行这个,而不是在家里(或者除了家庭之外,我想),但它被国防部严格控制了。实际上,这就是我自己滚动的部分原因。

无论如何我会写下来看看它是否有效。感谢大家!


缥缈止盈
浏览 674回答 3
3回答

湖上湖

我之前没有找到过这个,但是从任何应用程序都可以连接SessionSwitchEventHandler。显然你的应用程序需要运行,但只要它是:Microsoft.Win32.SystemEvents.SessionSwitch += new Microsoft.Win32.SessionSwitchEventHandler(SystemEvents_SessionSwitch);void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e){     if (e.Reason == SessionSwitchReason.SessionLock)     {          //I left my desk     }     else if (e.Reason == SessionSwitchReason.SessionUnlock)     {          //I returned to my desk     }}

阿波罗的战车

我将创建一个处理OnSessionChange事件的Windows服务(visual studio 2005项目类型),如下所示:protected override void OnSessionChange(SessionChangeDescription changeDescription){     if (changeDescription.Reason == SessionChangeReason.SessionLock)     {          //I left my desk     }     else if (changeDescription.Reason == SessionChangeReason.SessionUnlock)     {          //I returned to my desk     }}您在此时记录活动的内容和方式取决于您,但Windows服务可以快速方便地访问Windows事件,如启动,关闭,登录/退出以及锁定和解锁事件。

HUH函数

下面的解决方案使用Win32 API。在工作站被锁定时调用OnSessionLock,并在解锁时调用OnSessionUnlock。[DllImport("wtsapi32.dll")]private static extern bool WTSRegisterSessionNotification(IntPtr hWnd,int dwFlags);[DllImport("wtsapi32.dll")]private static extern bool WTSUnRegisterSessionNotification(IntPtrhWnd);private const int NotifyForThisSession = 0; // This session onlyprivate const int SessionChangeMessage = 0x02B1;private const int SessionLockParam = 0x7;private const int SessionUnlockParam = 0x8;protected override void WndProc(ref Message m){     // check for session change notifications     if (m.Msg == SessionChangeMessage)     {         if (m.WParam.ToInt32() == SessionLockParam)             OnSessionLock(); // Do something when locked         else if (m.WParam.ToInt32() == SessionUnlockParam)             OnSessionUnlock(); // Do something when unlocked     }     base.WndProc(ref m);     return;}void OnSessionLock() {     Debug.WriteLine("Locked...");}void OnSessionUnlock() {     Debug.WriteLine("Unlocked...");}private void Form1Load(object sender, EventArgs e){     WTSRegisterSessionNotification(this.Handle, NotifyForThisSession);}     // and then when we are done, we should unregister for the notification     //  WTSUnRegisterSessionNotification(this.Handle);
打开App,查看更多内容
随时随地看视频慕课网APP