111,076
社区成员




WatchKey wk = new WatchKey(this);
Thread thKey = new Thread(new ThreadStart(wk.StartWatch));
thKey.IsBackground = true;
thKey.Start();
//子线程中的timer,现在设置的是每隔1.5秒就去读取一次USB设备中的信息
void t_Elapsed(object sender, ElapsedEventArgs e)
{
tempKeyID = EPass.GetKeyID(); //读取USB设备的ID(USB本身ID,跟系统硬件ID无关)
if (!string.IsNullOrEmpty(tempKeyID.Trim()) && tempKeyID.IndexOf("0000") < 0)
{
//main是主窗体,在主窗体中用KeyID变量保存了USB设备ID值
if (main.KeyID != tempKeyID)
{
main.OnButton(true); //启用主窗体的几个按钮
main.SetKeyID(tempKeyID); //设置主窗体中的KeyID变量值为当前USB设备ID
main.ReadInfo(); //读取USB设备相关信息并在主窗体的RichtextBox中输出
main.SetThisText("KEY已连接"); //设置主窗体标题
//t.Stop();
}
}
else
{
//如果是此USB设备被拨下
main.OnButton(false); //禁用主窗体的几个按钮
main.KeyID = "";
main.SetThisText("KEY未连接");
}
}
/// <summary>
/// 启用/禁用操作按钮
/// </summary>
/// <param name="on"></param>
public void OnButton(bool on)
{
if (this.InvokeRequired)
{
OnButtonCallBack ocb = new OnButtonCallBack(OnButton);
this.Invoke(ocb, new object[] { on });
}
else
{
if (on)
{
btnWrite.Enabled = true;
btnRead.Enabled = true;
btnDelete.Enabled = true;
}
else
{
btnWrite.Enabled = false;
btnRead.Enabled = false;
btnDelete.Enabled = false;
}
}
}
/// <summary>
/// 设置KEY ID值.供委托调用
/// </summary>
/// <param name="keyID"></param>
public void SetKeyID(string keyID)
{
if (this.InvokeRequired)
{
SetKeyIDInvoke sk = new SetKeyIDInvoke(SetKeyID);
this.Invoke(sk, new object[] { keyID });
}
else
{
this.KeyID = keyID;
}
}
/// <summary>
/// 读取Key信息
/// </summary>
public void ReadInfo()
{
if (this.KeyID.Trim() != "" && this.KeyID.IndexOf("0000") < 0)
{
object[] obj = GetKeyInfo(this.KeyID); //根据keyid获取相关信息
if (obj[0] != null)
{
//WriteString是用来向Richtextbox输出字符的,用AppendText
WriteString("\n操作时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
WriteString("\n硬件标识:" + this.KeyID);
WriteString("\n店铺名称:" + GetAgentName(Convert.ToInt16(obj[0])));
WriteString("\n分店名称:" + GetChildName(Convert.ToInt16(obj[1])));
WriteString("\n修改时间:" + obj[2].ToString());
WriteString("\n" + new string('=', 49));
}
}
}
/// <summary>
/// 读取KEY硬件ID
/// </summary>
/// <returns></returns>
public string GetKeyID()
{
string key = EPass.GetStrProperty(7, 0, "").ToString().Trim();
if (key.IndexOf("0000") >= 0)
{
try
{
EPass.OpenDevice(1, "");
object obj = EPass.GetStrProperty(7, 0, "");
if (!string.IsNullOrEmpty(obj.ToString().Trim()))
{
key=obj.ToString().Trim();
}
else
{
key="";
}
}
catch
{
key="";
}
}
return key;
}
class WatchKey
{
private static Main main;
private EPass1000ND EPass = new EPass1000ND();
Timer t = new Timer(1500);
private string tempKeyID = "";
public WatchKey(Main m)
{
main = m;
}
public void StartWatch()
{
t.AutoReset = true;
t.Elapsed += new ElapsedEventHandler(t_Elapsed);
t.Start();
}
void t_Elapsed(object sender, ElapsedEventArgs e)
{
tempKeyID = EPass.GetKeyID();
if (!string.IsNullOrEmpty(tempKeyID.Trim()) && tempKeyID.IndexOf("0000") < 0)
{
if (main.KeyID != tempKeyID)
{
main.OnButton(true);
main.SetKeyID(tempKeyID);
main.ReadInfo();
main.SetThisText("KEY已连接");
//t.Stop();
}
}
else
{
main.OnButton(false);
main.KeyID = "";
main.SetThisText("KEY未连接");
}
}
}
/// <summary>
/// 读取KEY硬件ID
/// </summary>
/// <returns></returns>
public string GetKeyID()
{
try
{
EPass.OpenDevice(1, ""); //打开设备,只有打开设备后才能进行下一步操作
object obj = EPass.GetStrProperty(7, 0, ""); //获取设备ID
//EPass.CloseDevice();
if (!string.IsNullOrEmpty(obj.ToString().Trim()))
{
return obj.ToString().Trim();
}
else
{
return "";
}
}
catch
{
//没有插入设备
return "";
}
}
if (main.KeyID != tempKeyID)
{
main.OnButton(true); //启用主窗体的几个按钮
main.SetKeyID(tempKeyID); //设置主窗体中的KeyID变量值为当前USB设备ID
//main.ReadInfo(); //读取USB设备相关信息并在主窗体的RichtextBox中输出
//main.SetThisText("KEY已连接"); //设置主窗体标题
System.Windows.Forms.MessageBox.Show("我被执行了");
}
tempKeyID = EPass.GetKeyID();
这句里面的
if (main.KeyID != tempKeyID)
{
main.OnButton(true); //启用主窗体的几个按钮
main.SetKeyID(tempKeyID); //设置主窗体中的KeyID变量值为当前USB设备ID
main.ReadInfo(); //读取USB设备相关信息并在主窗体的RichtextBox中输出
main.SetThisText("KEY已连接"); //设置主窗体标题
//t.Stop();
}
tempKeyID = EPass.GetKeyID(); //读取USB设备的ID(USB本身ID,跟系统硬件ID无关)