Click here to Skip to main content
14,659,534 members
Rate this:
Please Sign up or sign in to vote.
See more:
Dear Sir, 
iam writing a windows service to get attendance log from attendance machine ,  iam using Visual Studio 2015 
and iam working on windows 10 64 bit  , the refence file name  is ZKEUEmKeeperNet.dll  version 1.0.0.0
runtime version v2.0.50727  actually iam facing 2 problems : 
 
1- iam getting partial log : i mean not all punch transactions appear (iam taking this data to DB Oracle )!!!!
i tried  i do not know why  but iam  removed the DB connection code and trying to write the data directly to harddisk  using writetofile function
 
2- iam not getting the log file on harddisk this is not an sdk problem as i think  this problem happened recently i do not know why!!
 
 any suggestions for this problem or is the database connection with orable caused a problem or the Onstart service time caused a problem iam running the service every 10 seconds and if i increased the time  problems happen like if  i make it every 100 seconds , please i need support it is taking too much time , thanks in advance 
 
below is my code:  
i tried the code

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 System.Threading.Tasks;
using Oracle.ManagedDataAccess.Client;
using Oracle.ManagedDataAccess;
namespace Att_Service2
{
public partial class Service1 : ServiceBase
{
ZkSoftwareEU.CZKEUEMNetClass axCZKEM1 = new ZkSoftwareEU.CZKEUEMNetClass();
private bool bIsConnected = false;//the boolean value identifies whether the device is connected
private int iMachineNumber = 1;//the serial number of the device.After connecting the device ,this value will be changed.
Timer timer = new Timer(); // name space(using System.Timers;)
string sdwEnrollNumber = "";
int idwTMachineNumber = 0;
int idwEMachineNumber = 0;
int idwVerifyMode = 0;
int idwInOutMode = 0;
int idwYear = 0;
int idwMonth = 0;
int idwDay = 0;
int idwHour = 0;
int idwMinute = 0;
int idwSecond = 0;
int idwWorkcode = 0;
int idwErrorCode = 0;
int iGLCount = 0;
int iIndex = 0;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
// WriteToFile("Service is started at " + DateTime.Now);
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = 10000; //10 seconds 
timer.Enabled = true;
}
protected override void OnStop()
{
// WriteToFile("Service is stopped at " + DateTime.Now);
}
public void WriteToFile(string Message)
{
string path = "D:\\Logs";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//AppDomain.CurrentDomain.BaseDirectory +
string filepath = "D:\\Logs\\ServiceLog_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";
if (!File.Exists(filepath))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(filepath))
{
sw.WriteLine(Message);
}
}
else
{
using (StreamWriter sw = File.AppendText(filepath))
{
sw.WriteLine(Message);
}
}
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
ZkSoftwareEU.CZKEUEMNetClass axCZKEM1 = new ZkSoftwareEU.CZKEUEMNetClass();
string TNS;
string IP;
//data in file should be like
//DATA SOURCE = 192.168.2.240:1521 / ORCL; USER ID = XXXXX; Password = YYYYY-192.168.2.74-
string totalData = File.ReadAllText(@"c:\Data\Att1.txt"); //read information for attendance Machine1
try
{
int index = totalData.IndexOf("-");
int lastindex = totalData.LastIndexOf("-");
int IPlength = lastindex - index - 1;
TNS = totalData.Substring(0, index).ToString().Trim();
IP = totalData.Substring(index + 1, IPlength).ToString().Trim();
}
catch (Exception ex) { WriteToFile("No file exist or revise the format to be like DATA SOURCE = 192.168.2.240:1521 / ORCL; USER ID = ASCON; Password = ASCON-192.168.2.74- the file path is c:\\data\\Att1.txt");return;}
/////////////////////////////////////////////////////////////////////////////////////
string sdwEnrollNumber = "";
int idwTMachineNumber = 0;
int idwEMachineNumber = 0;
int idwVerifyMode = 0;
int idwInOutMode = 0;
int idwYear = 0;
int idwMonth = 0;
int idwDay = 0;
int idwHour = 0;
int idwMinute = 0;
int idwSecond = 0;
int idwWorkcode = 0;
int idwErrorCode = 0;
int iGLCount = 0;
int iIndex = 0;
int Port = 4370;
bool bIsConnected;
int iMachineNumber = 1;
string insertSQL;
//3- Get Data From Attandance Machine
try 
{ 
while (axCZKEM1.Connect_Net(IP, Port) != true) ;//****New check connection TCP
WriteToFile("Connected to attendance "+DateTime.Now.ToString());
 
axCZKEM1.EnableDevice(iMachineNumber, false);//disable the device today
WriteToFile("Device Disabled " + DateTime.Now.ToString());
 
while (axCZKEM1.ReadGeneralLogData(iMachineNumber) != true) ;//**New i added a loop instead of if 
WriteToFile("Begin Loop " + DateTime.Now.ToString());
 
while (axCZKEM1.SSR_GetGeneralLogData(iMachineNumber, ref sdwEnrollNumber, ref idwVerifyMode,
ref idwInOutMode, ref idwYear, ref idwMonth, ref idwDay, ref idwHour, ref idwMinute, ref idwSecond, ref idwWorkcode))//get records from the memory
{
// //insert into Oracle DB
DateTime Date = Convert.ToDateTime(idwYear.ToString() + "-" + idwMonth.ToString() + "-" + idwDay.ToString() + " " + idwHour.ToString() + ":" + idwMinute.ToString() + ":" + idwSecond.ToString());
// WriteToFile(Date);
//MessageBox.Show(iMachineNumber + "-" + sdwEnrollNumber + "-" + Date);
/*Commented on 18-7-2020
WriteToFile(iMachineNumber+"-"+ sdwEnrollNumber+"-"+ idwInOutMode+"-"+ Date);
*/
// MessageBox.Show("inserted Successfully");
}//end while
}//end try
 catch (Exception ex)
 { 
  WriteToFile("error " + ex.ToString()+ " at "+DateTime.Now.ToString() );
 }//today
}//end function
}//end class
}//end namespace


What I have tried:

i have tried the above code please any suggestions
Posted
Updated 11hrs 20mins ago

Rate this:
Please Sign up or sign in to vote.

Solution 1

At a guess - and without being able to run that code under the same conditions you do that's all it can be - it's a permissions problem.

Unless the folder that the app is trying to write to has write / create permissions for teh user the app is running as then that code will fail.

What I'd start be doing is adding some try...catch blocks to WriteToFile to monitor what is going on and give you message boxes or similar to track what errors are happening, and use the debugger to find out exactly what the code is doing.

Without that, it's all guesswork!
   
Comments
Member 13639193 31 mins ago
   
could the antivirus cause the same problem
Member 13639193 23 mins ago
   
can anything stops my service and restart it again so there is a timegap between them
Rate this:
Please Sign up or sign in to vote.

Solution 2

Dear sir , thanks for answering , i will add the write to file between try /catch and test
again ,thanks for illumination

also iam asking , if the antivirus can affect my windows service ??
   
v2
Comments
Member 13639193 19 mins ago
   
also can the permission of the file , cause not to write data , also is there any certain answer about it .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100