ZKemKeeper库中的实时事件处理程序没有响应

时间:2016-04-20 05:31:12

标签: c#

我创建了一个Windows服务,通过使用Interop.zkemkeeper库从指纹设备使用实时事件来获取出勤率。在服务中机器已成功连接但服务未响应OnAttTransactionEx实时事件意味着在使用OnAttTransactionEx事件未获取成功连接到机器出勤之后。我不知道是什么问题。

以下是Windows服务的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.IO;
using zkemkeeper;
using System.Threading;
using System.Windows.Forms;

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
       // private System.Timers.Timer timer1 = null;
        string filePath = @"E:\file1.txt";
        bool connSatus = false;
        CZKEMClass axCZKEM1;

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            /* timer1 = new Timer();
             this.timer1.Interval = 10000;
             this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
             timer1.Enabled = true;
           */
              axCZKEM1 = new zkemkeeper.CZKEMClass();
              Thread createComAndMessagePumpThread = new Thread(() =>
              {
                  connSatus = axCZKEM1.Connect_Net("192.169.9.34", 4370);
                  using (StreamWriter writer = new StreamWriter(filePath, true))
                  {
                      writer.WriteLine("Machine is connected on" + "Date :" + DateTime.Now.ToString() + "status" + connSatus);
                      writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
                  }

                  if (connSatus == true)
                  {

                      this.axCZKEM1.OnAttTransactionEx -= new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx);

                      if (axCZKEM1.RegEvent(1, 65535))//Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all)
                      {

                          this.axCZKEM1.OnAttTransactionEx += new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx);
                          using (StreamWriter writer = new StreamWriter(filePath, true))
                          {
                              writer.WriteLine("finger print Event is registered... ");
                              writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
                          }

                      }
                  }

                 Application.Run();
              });
              createComAndMessagePumpThread.SetApartmentState(ApartmentState.STA);

              createComAndMessagePumpThread.Start();


        }
        public 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)
        {
           // if (OnAttTransactionEx != null) OnAttTransactionEx(sEnrollNumber, iIsInValid, iAttState, iVerifyMethod, iYear, iMonth, iDay, iHour, iMinute, iSecond, iWorkCode, axCZKEM1.MachineNumber, Tag);
            using (StreamWriter writer = new StreamWriter(filePath, true))
            {
                writer.WriteLine(" OnAttTrasactionEx Has been Triggered,Verified OK on" + "Date :" + "Enrollnumber" + sEnrollNumber + DateTime.Now.ToString());
                writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
            } 
        }


        protected override void OnStop()
        {
           // timer1.Enabled = false;
            this.axCZKEM1.OnAttTransactionEx -= new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx);
            axCZKEM1.Disconnect();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            using (StreamWriter writer = new StreamWriter(filePath, true))
            {
                writer.WriteLine("Message is running on" + "Date :" + DateTime.Now.ToString());
                writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

corret version就是那个

protected override void OnStart(string[] args)
        {

              Thread createComAndMessagePumpThread = new Thread(() =>
              {
                  axCZKEM1 = new zkemkeeper.CZKEMClass();
                  connSatus = axCZKEM1.Connect_Net("192.169.9.34", 4370);
                  using (StreamWriter writer = new StreamWriter(filePath, true))
                  {
                      writer.WriteLine("Machine is connected on" + "Date :" + DateTime.Now.ToString() + "status" + connSatus);
                      writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
                  }

                  if (connSatus == true)
                  {

                      this.axCZKEM1.OnAttTransactionEx -= new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx);

                      if (axCZKEM1.RegEvent(1, 65535))//Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all)
                      {

                          this.axCZKEM1.OnAttTransactionEx += new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx);
                          using (StreamWriter writer = new StreamWriter(filePath, true))
                          {
                              writer.WriteLine("finger print Event is registered... ");
                              writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
                          }

                      }
                  }

                 Application.Run();
              });
              createComAndMessagePumpThread.SetApartmentState(ApartmentState.STA);

              createComAndMessagePumpThread.Start();


        }