Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 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

C#
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 27-Oct-20 20:12pm

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!
 
Share this answer
 
Comments
Member 13639193 20-Oct-20 2:50am    
could the antivirus cause the same problem
Member 13639193 20-Oct-20 2:58am    
can anything stops my service and restart it again so there is a timegap between them
I suspect your "timer-based" solution is causing problems. Why not use a FileSystemWatcher, observe the file(s) you need for change and react as you do now on elapsed event. Even if this isn't the root of your current problem, this will make your code much less error-prone.

Guess: I think someone is writing to the file while you try to read it, and it's locked. so open the file with readonly permission...

Another suggestions: Make WriteToFile more lightweight, create directory etc. before that and don't check on every write.


P.S. Antivirus doesn't behave like that, thats not your problem...
 
Share this answer
 
v2
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 ??
 
Share this answer
 
v2
Comments
Member 13639193 20-Oct-20 3:02am    
also can the permission of the file , cause not to write data , also is there any certain answer about it .
Member 13639193 21-Oct-20 2:38am    
actually i tried to give permission to the file but the problem persists

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900