最近在倒騰中控指紋儀,用的是官方提供的sdk 地址
??? 在使用過程中需要對進(jìn)出記錄進(jìn)行實(shí)時監(jiān)控,開發(fā)包也有這個功能,不使用多線程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))//打開指紋儀實(shí)時事件功能
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("...驗(yàn)證方式:" + 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 }
不使用多線程連接一臺指紋儀
??? 但如果連接多臺設(shè)備就必須用多個線程(如使用單線程一臺指紋儀連接有問題就會整個卡死),一個線程對應(yīng)一個指紋儀,但線程執(zhí)行完了就回收了,無法響應(yīng)事件或用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到底是什么原理,它實(shí)例化之后是否就附加在實(shí)例化它的那個線程上?線程回收了,它也就回收了?
?? 這SDK是否自己會新建線程用于響應(yīng)事件?為什么在winform下就能響應(yīng),而新建線程就不行呢?
??本人野路子,沒有學(xué)習(xí)過,純屬個人愛好,不要吐槽以上文字,理解意思就行。。????
中控指紋儀二次開發(fā)遇到的問題
30秒到達(dá)戰(zhàn)場
2018-12-07 06:39:24
