84 lines
2.2 KiB
C#
84 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Windows.Forms;
|
|
|
|
using CommonLib;
|
|
|
|
namespace Lskj.Main
|
|
{
|
|
/// <summary>
|
|
/// 获取系统事件
|
|
/// </summary>
|
|
public class ServerTimer
|
|
{
|
|
private static DateTime m_CurTime = DateTime.Now;
|
|
|
|
private static Timer m_runTmr;
|
|
|
|
public static void StartTimer()
|
|
{
|
|
if (m_runTmr != null)
|
|
|
|
QueryTime();
|
|
m_runTmr = new Timer();
|
|
m_runTmr.Tag = 0;
|
|
m_runTmr.Tick += new EventHandler(OnTimer);
|
|
m_runTmr.Interval = 1000;
|
|
m_runTmr.Start();
|
|
|
|
}
|
|
|
|
public static DateTime ServerTime
|
|
{
|
|
get { return m_CurTime; }
|
|
}
|
|
|
|
private static void OnTimer(object sender, EventArgs e)
|
|
{
|
|
m_CurTime = m_CurTime.AddSeconds(1);
|
|
int count = (int)m_runTmr.Tag;
|
|
count++;
|
|
|
|
//暂定30秒查询一次
|
|
if (count > 30)
|
|
{
|
|
count = 0;
|
|
QueryTime();
|
|
}
|
|
m_runTmr.Tag = count;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询服务器时间,并查询登录表记录,没记录则强制退出
|
|
/// </summary>
|
|
private static void QueryTime()
|
|
{
|
|
try
|
|
{
|
|
string strSql = string.Format("select getdate() as cnt from p_LoginHostInfotab where HostName='{0}'", ERPInfo.Instance.WSName);
|
|
using (SqlDataReader dr = SqlHelper.ExecuteReader(CommandType.Text, strSql, null))
|
|
{
|
|
if (dr.Read())
|
|
{
|
|
m_CurTime = DateTime.Parse(dr["cnt"].ToString());
|
|
}
|
|
else
|
|
{
|
|
//掉线
|
|
//MessageBox.Show("你已经被强制下线.");
|
|
//Application.Exit();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
PubUtil.WriteLog(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|