如果您在车间使用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. 为什么vue中的data用return返回呢?

    不使用return包裹的数据会在项目的全局可见,会造成变量污染:使用return包裹后数据中变量只在当前组件中生效,不会影响其他组件. 当一个组件被定义, data 必须声明为返回一个初始数据对象的函 ...

  2. 建议20:建议通过Function扩展类型

    JavaScript允许为语言的基本数据类型定义方法.通过Object.prototype添加原型方法,该方法可被所有的对象,.这样的方法对函数,数组,字符串,数字,正则表达式和布尔值都适用.例如,通 ...

  3. 组件/ 外层数据初始化时候,不应该触发 on-change 事件

    组件/ 外层数据初始化时候,不应该触发 on-change 事件 watch: { value (value) { this.noOnChange = true // 外层传值 不触发on-chang ...

  4. spring总结————AOP面向切面总结

    spring总结————AOP面向切面 一.spring aop概念 spring aop面向切面编程,java是面向对象的语言. 真正的service层代码 业务逻辑层再处理业务之前和之后都要进行一 ...

  5. 3D游戏中各种空间变换到底是怎么回事

    每一个游戏可以呈现炫丽效果的背后,需要进行一系列的复杂计算,同时也伴随着各种各样的顶点空间变换.渲染游戏的过程可以理解成是把一个个顶点经过层层处理最终转化到屏幕上的过程,本文就旨在说明,顶点是经过了哪 ...

  6. Postgresql实战经验之alter table 开小差了

    Postgresql实战经验之alter table 开小差了 今天需要将一张有数据的表中一个字段varchar 类型转换为timestamp类型,但是pg的alter table 语句却开小差,出现 ...

  7. 泛微E-cology OA 远程代码执行漏洞

    分析文章:https://dwz.cn/bYtnsKwa http://127.0.0.1/weaver/bsh.servlet.BshServlet 若存在如上页面,则用下面数据包进行测试. POS ...

  8. java基础-对集合(Collection)的总结

    集合(Collection)类型也是Java标准库中被使用最多的类型. List ArrayList LinkedList 对于自定义的对象,需要正确覆写 equals方法 参看7.3 例子 Map ...

  9. [Docker8]Dockerfiles

    Comment INSTRUCTION arguments FROM 基于哪个base镜像 RUN 执行命令并创建新的镜像层,run经常用于安装软件包 MAINTAINER 镜像创建者 copy 将文 ...

  10. CF codeforces A. New Year Garland【Educational Codeforces Round 79 (Rated for Div. 2)】

    A. New Year Garland time limit per test 1 second memory limit per test 256 megabytes input standard ...