我创建了一个简单的Windows服务项目,使用ZKemKeeper库从指纹设备捕获实时事件。该服务似乎在第一时间正常工作,并且它对设备中的事件做出了很好的响应。但我发现,服务在几分钟不活动后停止响应该事件。我认为是导致问题的服务,但是WinForm应用程序中的问题仍然存在,甚至SDK中包含的示例都显示了确切的问题。
以下是Windows服务的代码:
public partial class Service1 : ServiceBase
{
private System.Diagnostics.EventLog eventLog1;
CZKEMClass myCZKEMClass;
public Service1()
{
InitializeComponent();
this.eventLog1 = new System.Diagnostics.EventLog();
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
this.eventLog1.Log = "MyLog";
this.eventLog1.Source = "MyLogSource";
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();
this.ServiceName = "ZKemKeeperTestService";
}
protected override void OnStart(string[] args)
{
myCZKEMClass = new CZKEMClass();
Thread createComAndMessagePumpThread = new Thread(() =>
{
myCZKEMClass.Connect_Net("192.168.1.20", 4370);
if (myCZKEMClass.RegEvent(1, 65535))
{
myCZKEMClass.OnAttTransactionEx += new _IZKEMEvents_OnAttTransactionExEventHandler(myCZKEMClass_OnAttTransactionEx);
}
Application.Run();
});
createComAndMessagePumpThread.SetApartmentState(ApartmentState.STA);
createComAndMessagePumpThread.Start();
eventLog1.WriteEntry("Service Started");
}
protected override void OnStop()
{
eventLog1.WriteEntry("Service Stopped");
myCZKEMClass.OnAttTransactionEx -= new _IZKEMEvents_OnAttTransactionExEventHandler(myCZKEMClass_OnAttTransactionEx);
}
private void myCZKEMClass_OnAttTransactionEx(string EnrollNumber, int IsInValid, int AttState, int VerificationMethod, int Year, int Month, int Day, int Hour, int Minute, int Second, int WorkCode)
{
eventLog1.WriteEntry(EnrollNumber.ToString());
}
}
我不知道问题的原因是什么,我被困在那里。仅供参考,我使用的是在Windows 10中运行的64位版本的库。我使用.NET Framework 4.0作为目标框架。
Achmad Mulyadi
答案 0 :(得分:5)
我遇到同样的问题,请尝试取消注册事件,而不是每60秒再次注册,这对我来说很有用。我正在使用64位版本和Windows 8.1中运行的库
if (bIsConnected == true)
{
this.axCZKEM1.OnFinger -= new zkemkeeper._IZKEMEvents_OnFingerEventHandler(axCZKEM1_OnFinger);
this.axCZKEM1.OnVerify -= new zkemkeeper._IZKEMEvents_OnVerifyEventHandler(axCZKEM1_OnVerify);
this.axCZKEM1.OnAttTransactionEx -= new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx);
this.axCZKEM1.OnFingerFeature -= new zkemkeeper._IZKEMEvents_OnFingerFeatureEventHandler(axCZKEM1_OnFingerFeature);
this.axCZKEM1.OnEnrollFingerEx -= new zkemkeeper._IZKEMEvents_OnEnrollFingerExEventHandler(axCZKEM1_OnEnrollFingerEx);
iMachineNumber = 1;//In fact,when you are using the tcp/ip communication,this parameter will be ignored,that is any integer will all right.Here we use 1.
if (axCZKEM1.RegEvent(iMachineNumber, 65535))//Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all)
{
this.axCZKEM1.OnFinger += new zkemkeeper._IZKEMEvents_OnFingerEventHandler(axCZKEM1_OnFinger);
this.axCZKEM1.OnVerify += new zkemkeeper._IZKEMEvents_OnVerifyEventHandler(axCZKEM1_OnVerify);
this.axCZKEM1.OnAttTransactionEx += new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx);
this.axCZKEM1.OnFingerFeature += new zkemkeeper._IZKEMEvents_OnFingerFeatureEventHandler(axCZKEM1_OnFingerFeature);
this.axCZKEM1.OnEnrollFingerEx += new zkemkeeper._IZKEMEvents_OnEnrollFingerExEventHandler(axCZKEM1_OnEnrollFingerEx);
}
}
答案 1 :(得分:1)
如果你仍然没有找到答案make Timer并将此代码放入其中
System.Windows.Forms.Application.DoEvents();
这将使ActiveX工作
答案 2 :(得分:0)
您可以尝试在线程中添加此代码。
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += new System.Timers.ElapsedEventHandler((object sender, System.Timers.ElapsedEventArgs e) =>
{
string IPAddr = null;
if (axCZKEM1.GetDeviceIP(iMachineNumber, IPAddr))
{
LogHelper.Log(LogLevel.Debug, "device " + IPAddr + ":" + port + " connect status is ok.");
}
});
timer.Interval = 600000;// 10 minutes
timer.Enabled = true;
答案 3 :(得分:0)
反复阅读手册中的解决方案后,我发现我的问题是防火墙,请尝试禁用防火墙或为您的软件添加例外