最近在倒腾中控指纹仪,用的是官方提供的sdk 地址
在使用过程中需要对进出记录进行实时监控,开发包也有这个功能,不使用多线程winform下连接一台指纹仪正常,可以反馈事件,代码如下
1 namespace AttLogs
2 {
3 public partial class AttLogsMain : Form
4 {
5 public AttLogsMain()
6 {
7 InitializeComponent();
8 }
9
10 //Create Standalone SDK class dynamicly.
11 public zkemkeeper.CZKEMClass axCZKEM1 = new zkemkeeper.CZKEMClass();
12
13 #region 连接指纹仪
14 private void btnConnect_Click(object sender, EventArgs e)
15 {
16 if (txtIP.Text.Trim() == "" || txtPort.Text.Trim() == "")
17 {
18 MessageBox.Show("IP and Port cannot be null", "Error");
19 return;
20 }
21 int idwErrorCode = 0;
22
23 Cursor = Cursors.WaitCursor;
24
25 bIsConnected = axCZKEM1.Connect_Net(txtIP.Text, Convert.ToInt32(txtPort.Text));
26 if (bIsConnected == true)
27 {
28 btnConnect.Text = "DisConnect";
29 btnConnect.Refresh();
30 lblState.Text = "Current State:Connected";
31 iMachineNumber = 1;
32 if (axCZKEM1.RegEvent(iMachineNumber, 65535))//打开指纹仪实时事件功能
33 {
34 //注册事件
35 this.axCZKEM1.OnAttTransactionEx += new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx);
36 this.axCZKEM1.OnFingerFeature += new zkemkeeper._IZKEMEvents_OnFingerFeatureEventHandler(axCZKEM1_OnFingerFeature);
37 }
38 }
39 else
40 {
41 axCZKEM1.GetLastError(ref idwErrorCode);
42 MessageBox.Show("Unable to connect the device,ErrorCode=" + idwErrorCode.ToString(), "Error");
43 }
44 Cursor = Cursors.Default;
45
46 Cursor = Cursors.Default;
47 }
48 #endregion
49
50 #region 事件处理
51
52 private void axCZKEM1_OnAttTransactionEx(string sEnrollNumber, int iIsInValid, int iAttState, int iVerifyMethod, int iYear, int iMonth, int iDay, int iHour, int iMinute, int iSecond, int iWorkCode)
53 {
54 lbRTShow.Items.Add("RTEvent OnAttTrasactionEx Has been Triggered,Verified OK");
55 lbRTShow.Items.Add("...用户ID:" + sEnrollNumber);
56 lbRTShow.Items.Add("...验证方式:" + iIsInValid.ToString());
57 lbRTShow.Items.Add("...attState:" + iAttState.ToString());
58 lbRTShow.Items.Add("...VerifyMethod:" + iVerifyMethod.ToString());
59 lbRTShow.Items.Add("...工号:" + iWorkCode.ToString());//the difference between the event OnAttTransaction and OnAttTransactionEx
60 lbRTShow.Items.Add("...时间:" + iYear.ToString() + "-" + iMonth.ToString() + "-" + iDay.ToString() + " " + iHour.ToString() + ":" + iMinute.ToString() + ":" + iSecond.ToString());
61 }
62 #endregion
63 }
64 }
不使用多线程连接一台指纹仪
但如果连接多台设备就必须用多个线程(如使用单线程一台指纹仪连接有问题就会整个卡死),一个线程对应一个指纹仪,但线程执行完了就回收了,无法响应事件或用while(true) sleep(1000)阻止回收也不行
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Threading;
5 using zkemkeeper;
6
7 namespace ConsoleMThreads
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 Thread thread = new Thread(new ThreadA().test);
14 thread.Start();
15 Console.ReadKey();
16 }
17 }
18
19
20 public class ThreadA
21 {
22 public zkemkeeper.CZKEMClass sdk = new CZKEMClass();//create Standalone SDK class dynamicly
23
24 public void test()
25 {
26 bIsConnected = axCZKEM1.Connect_Net("192.168.1.1", 4370);
27 if (bIsConnected == true)
28 {
29 iMachineNumber = 1;
30 if (axCZKEM1.RegEvent(iMachineNumber, 65535))
31 {
32 this.axCZKEM1.OnAttTransactionEx += new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx);
33 Console.WriteLine("Connected");
34 }
35 else
36 {
37 Console.WriteLine("Error");
38 }
39 }
40
41 //加不加都不行
42 //while(true)
43 //{
44 // sleep(1000);
45 //}
46 }
47
48 private void axCZKEM1_OnAttTransactionEx(string sEnrollNumber, int iIsInValid, int iAttState, int iVerifyMethod, int iYear, int iMonth, int iDay, int iHour, int iMinute, int iSecond, int iWorkCode)
49 {
50 Console.WriteLine("RTEvent OnAttTrasactionEx Has been Triggered,Verified OK");
51 Console.WriteLine("...UserID:" + sEnrollNumber);
52 Console.WriteLine("...isInvalid:" + iIsInValid.ToString());
53 Console.WriteLine("...attState:" + iAttState.ToString());
54 Console.WriteLine("...VerifyMethod:" + iVerifyMethod.ToString());
55 Console.WriteLine("...Workcode:" + iWorkCode.ToString());//the difference between the event OnAttTransaction and OnAttTransactionEx
56 Console.WriteLine("...Time:" + iYear.ToString() + "-" + iMonth.ToString() + "-" + iDay.ToString() + " " + iHour.ToString() + ":" + iMinute.ToString() + ":" + iSecond.ToString());
57 }
58 }
59
60 }
新建线程连接(记事本写的,可能有错,就是这个意思)
这个SDK到底是什么原理,它实例化之后是否就附加在实例化它的那个线程上?线程回收了,它也就回收了?
这SDK是否自己会新建线程用于响应事件?为什么在winform下就能响应,而新建线程就不行呢?
本人野路子,没有学习过,纯属个人爱好,不要吐槽以上文字,理解意思就行。。
24 回答
潇湘沐
TA贡献1816条经验 获得超6个赞
@Launcher: 也就是说STA COM里有一个隐藏的消息队列?
这个指定线程为STA的操作之前调试的时候就是有的,还是不行
如果仅仅调用方法,都是没问题的,就是这个事件响应不了
慕工程0101907
TA贡献1887条经验 获得超5个赞
@zhr008:
首先我这个是window服务运行的,用winfrom窗口是有实时事件的,但服务不行,按照你这个也不见有触发。
什么问题啊?
- 24 回答
- 0 关注
- 1919 浏览
添加回答
举报
0/150
提交
取消