MES Auto Logout

如果您在车间使用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()

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

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

原文地址:https://www.cnblogs.com/Wang-lee/p/12689454.html