如果您在车间使用MES,可能存在这种情况有人在仍然登录的情况下偶尔离开终端。如果一段时间不使用终端,我们是否可以让用户自动注销。

1 首先,我们有一条using语句:

using System.Runtime.InteropServices

2 添加2个新类。

public class IdleEventArgs : EventArgs {

        private DateTime m_EventTime;

        public DateTime EventTime {
get {
return m_EventTime;
}
} public IdleEventArgs(DateTime timeOfEvent) {
m_EventTime = timeOfEvent;
}
} public class SystemIdleTimer : Component
{
private const double INTERNAL_TIMER_INTERVAL = 550;
[Description("Event that if fired when idle state is entered.")]
public event OnEnterIdleStateEventHandler OnEnterIdleState;
public delegate void OnEnterIdleStateEventHandler(object sender, IdleEventArgs e);
[Description("Event that is fired when leaving idle state.")]
public event OnExitIdleStateEventHandler OnExitIdleState;
public delegate void OnExitIdleStateEventHandler(object sender, IdleEventArgs e); private System.Timers.Timer ticker;
private int m_MaxIdleTime;
private object m_LockObject; private bool m_IsIdle = false; [Description("Maximum idle time in seconds.")]
public int MaxIdleTime {
get { return m_MaxIdleTime; }
set {
if (value == 0) {
throw new ArgumentException("MaxIdleTime must be larger then 0.");
} else {
m_MaxIdleTime = value;
}
}
}
public SystemIdleTimer()
{
m_LockObject = new object();
ticker = new System.Timers.Timer(INTERNAL_TIMER_INTERVAL);
ticker.Elapsed += InternalTickerElapsed;
}
public void Start() public void Stop()
{
ticker.Stop();
lock (m_LockObject) {
m_IsIdle = false;
}
}
public bool IsRunning {
get { return ticker.Enabled; }
}
private void InternalTickerElapsed(object sender, System.Timers.ElapsedEventArgs e)
{
uint idleTime = Win32Wrapper.GetIdle();
if (idleTime > (MaxIdleTime * 1000)) {
if (m_IsIdle == false) {
lock (m_LockObject) {
m_IsIdle = true;
}
IdleEventArgs args = new IdleEventArgs(e.SignalTime);
if (OnEnterIdleState != null) {
OnEnterIdleState(this, args);
}
}
} else {
if (m_IsIdle) {
lock (m_LockObject) {
m_IsIdle = false;
}
IdleEventArgs args = new IdleEventArgs(e.SignalTime);
if (OnExitIdleState != null) {
OnExitIdleState(this, args);
}
}
}
}
} public class Win32Wrapper
{
public struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
} [DllImport("User32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO lii); public static uint GetIdle()
{
LASTINPUTINFO lii = new LASTINPUTINFO();
lii.cbSize = Convert.ToUInt32((Marshal.SizeOf(lii)));
GetLastInputInfo(ref lii);
return Convert.ToUInt32(Environment.TickCount) - lii.dwTime;
}
}

3 声明一新计时器对象的实例:

SystemIdleTimer idleTimer = new SystemIdleTimer();

4 添加事件以处理用户何时空闲以及何时返回:

    private void idleTimer_OnEnterIdleState(object sender, IdleEventArgs args)
{
// This is where you put the code to deal with when the session has been detected
// as entering an idle state
MessageBox.Show("Get back to work Adam!");
idleTimer.Start();
} private void idleTimer_OnExitIdleState(object sender, IdleEventArgs args)
{
// This is where you put the code for when the session was idle and now has
// been re-activated. You might not need this for anything, but thought
// it would be good to point out.
MessageBox.Show("Good boy");
idleTimer.Start();
}
5 在表格加载时,将这些事件连接起来,并定义不活动的秒数,也可以根据具体情况进行调整:
private void MESMenu_Load(object sender, EventArgs args)
{
idleTimer.OnEnterIdleState += new SystemIdleTimer.OnEnterIdleStateEventHandler(idleTimer_OnEnterIdleState);
idleTimer.OnExitIdleState += new SystemIdleTimer.OnExitIdleStateEventHandler(idleTimer_OnExitIdleState);
idleTimer.MaxIdleTime = 10; // Seconds
idleTimer.Start();
}
6 最后,清理要处置的对象:
public void DestroyCustomCode()
{
// ** Wizard Insert Location - Do not delete 'Begin/End Wizard Added Object Disposal' lines **
// Begin Wizard Added Object Disposal // End Wizard Added Object Disposal // Begin Custom Code Disposal
idleTimer.OnEnterIdleState -= new SystemIdleTimer.OnEnterIdleStateEventHandler(idleTimer_OnEnterIdleState);
idleTimer.OnExitIdleState -= new SystemIdleTimer.OnExitIdleStateEventHandler(idleTimer_OnExitIdleState);
idleTimer.Dispose();
// End Custom Code Disposal
}
现在您可以将此客制化保存到MES,运行它。如果10秒不移动鼠标会提醒您"Get Back To Work Adam", 只要您移动鼠标会提示"Good boy":

7 更新了OnEnterIdleState逻辑以调用LogOut,LogOut只是获取对按钮的引用并单击它:
private void idleTimer_OnEnterIdleState(object sender, IdleEventArgs args)
{
// This is where you put the code to deal with when the session has been detected
// as entering an idle state
this.LogOut();
idleTimer.Start();
} private void LogOut()
{
EpiButton btnLogOut = (EpiButton)csm.GetNativeControlReference("a2f6e795-4ab3-4121-bce4-e1d5f0881b0a");
btnLogOut.PerformClick();
} // LogOut()

如果这个博客对您有帮助,请帮忙给评论,谢谢!

如果您正在看这个并且意识到自己想要类似的东西,请联系我,谢谢!

MES Auto Logout的更多相关文章

  1. 关于linux系统安全配置脚本

    本脚本是第二次更新,已经大量应用在某大型媒体网站体系中,加入了之前没有想到的一些安全设置.使用方法将其复制,保存为一个shell文件,比如security.sh.将其上传到Linux服务器上,执行sh ...

  2. RZ10

    设定一些系统参数     例如在生成table maintenance的时候 由于表格结构复杂 导致生成维护程序时 超出了默认的内存限制 这时候可以通过RZ10 修改 zzta/dynpro_area ...

  3. linux运维安全工具集合[持续更新中..]

    lynis 安全漏洞检测工具    https://cisofy.com/download/lynis/    https://cisofy.com/files/lynis-2.2.0.tar.gz ...

  4. Linux的系统安全设置Shell脚本

    #!/bin/sh # desc: setup linux system security # powered by www.lvtao.net #account setup passwd -l xf ...

  5. I fullly understand why can not set "auto commit off" in sqlserver

    This is xxxxx Because MES guy mistaken , the data was wrong and made system error then. After that I ...

  6. C++11特性——变量部分(using类型别名、constexpr常量表达式、auto类型推断、nullptr空指针等)

    #include <iostream> using namespace std; int main() { using cullptr = const unsigned long long ...

  7. overflow:hidden与margin:0 auto之间的冲突

    相对于父容器水平居中的代码margin:0 auto与overflow:hidden之间存在冲突.当这两个属性同时应用在一个DIV上时,在chrome浏览器中将无法居中.至于为啥我也不明白.

  8. Android Auto开发之一《开始学习Auto 》

    共同学习,共同进步, 转载请注明出处.欢迎微信交流:sfssqs,申请注明"Android Car"字样 ================= =================== ...

  9. width:100%;与width:auto;的区别

    <div> <p>1111</p> </div> div{ width:980px; background-color: #ccc; height:30 ...

随机推荐

  1. pip 安装超时问题

    pip install -i https://pypi.doubanio.com/simple/ 包名 参考: https://blog.csdn.net/qq_39161804/article/de ...

  2. JAVAEE学习day05学习,数组

    容器及元素的概念 容器:是将多个数据存储到一起 元素:每个数据称为该容器的元素 数组的概念 数组:数组是长度固定,存储数据的容器,保证多个数据的类型要一致 数组定义格式及其描述 动态定义: 数据类型 ...

  3. LeetCode(不用加号的加法)

    题目: 设计一个函数把两个数相加,不得使用+或者其他算数运算符. 示例: 输入:a=1,b=1 输出:2 提示: a,b均有可能是负数或0 结果不会溢出32位整数 初始思路: 看到题目我就明白只能用位 ...

  4. iview admin template 基础模板架子

    https://github.com/iview/iview-admin/tree/template

  5. Python专题——详解enumerate和zip

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是Python专题的第7篇文章,我们继续介绍迭代相关. enumerate 首先介绍的是enumerate函数. 在我们日常编程的过程当 ...

  6. js遍历删除对象的key

    // 如果用户没有填写值,则删除对象的key. Object.keys(obj).forEach( (key) => {      if (!obj[key]) { // !obj[key]表示 ...

  7. Swift 4.0 高级-自定义操作符

    在Swift语言中,常见的操作符有+.-.*./.>.<.==.&&.||等等,如果不喜欢,你也可以定义自己喜欢的操作符. 操作符类型 中置运算符(infix operat ...

  8. 【i春秋综合渗透测试】《我很简单,请不要欺负我》

      第2题:获取目标网站管理员的密码 扫到了后台(/admin),本来想用sqlmap跑一下,但是随便试了个弱口令(admin888)就进去了...   第3题: getshell 配置插马:登录后台 ...

  9. NDCG的理解

    2019-05-29 14:15:44

  10. Django HttpResponse

    HttpResponse 概述:给浏览器返回数据 HttpRequest对象是由django创建的,HttpResponse对象由程序员创建 用法 1:不调用模板,直接返回数据. 例: def get ...