C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2712
Number of posts: 5738

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
c# application hang.plz help Posted by spidervikas on 8 May 2011 at 11:15 PM
code is

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RfidApiLib;
using System.IO;
using System.Data.SqlClient;
using System.Threading;
namespace Service_Ambu_Exit
{
public partial class Form1 : Form
{
RfidApi api = new RfidApi();
public int TagCnt = 0;
string ip;
string portNo;
string p_type = "";
string p1 = "";
string arr1;
int intcount = 0;
public Form1()
{
InitializeComponent();
}






private int connectReader()
{
try
{
StreamReader sr1 = new StreamReader("ipandport.txt");
string[] str2;
string[] str3;
string arr3;
arr3 = sr1.ReadLine();
str2 = arr3.Split('=');
ip = str2[1].Trim();



StreamReader sr3 = new StreamReader("ipandport.txt");
string arr2;
arr2 = sr3.ReadLine();
arr2 = sr3.ReadLine();
str3 = arr2.Split('=');
portNo = str3[1].Trim();




int status;
int port;

string s = "";
try
{
port = int.Parse(portNo);
s = ip;

}
catch (Exception)
{
logfile("Please mention the valid ip address and port in settings file.");
return 0;
}
status = api.TcpConnectReader(ip, port);


if (status != 0)
{
logfile("Connect Reader Failed.");
return 0;
}


if (ip == "172.16.100.3".Trim() || ip == "172.16.100.4".Trim())
{
p1 = "STAFF".Trim();
}
else if (ip == "172.16.100.5".Trim() || ip == "172.16.100.7".Trim() || ip == "172.16.100.6".Trim())
{
p1 = "AMBULANCE".Trim();
}
else if (ip == "172.16.100.2".Trim() || ip == "172.16.100.1".Trim())
{
p1 = "BASEMENT".Trim();
}

StreamReader sr = new StreamReader("RFSettings.txt");
string[] str1;
string arr;
arr = sr.ReadLine();
str1 = arr.Split(':');
arr1 = str1[1].Trim();


}
catch (Exception e)
{
logfile(e.Message.ToString());
} return 1;

}

private void Form1_Load(object sender, EventArgs e)
{

try
{

int i = 0;
while (i == 0)
{
i = connectReader();
// Thread.Sleep(2000);

}
logfile("Reader Connected.");
timer1.Start();
//timer2.Start();

}
catch (Exception e1)
{
logfile(e1.Message.ToString());

}
}

private void logfile(string str)
{
try
{
string month = string.Format("{0:MM}", DateTime.Now);
string year = string.Format("{0:yyyy}", DateTime.Now);
if (Directory.Exists("log/" + year + ""))
{

}
else
{
Directory.CreateDirectory("log/" + year + "");
}

if (Directory.Exists("log/" + year + "/" + month + ""))
{

}
else
{
Directory.CreateDirectory("log/" + year + "/" + month + "");
}




if (Directory.Exists("log/" + year + "/" + month + ""))
{

StreamWriter sw; //= new StreamWriter("log/" + year + "/" + month + "/RFlog.txt");
if (File.Exists("log/" + year + "/" + month + "/RFlog.txt"))
{
string str2 = DateTime.Now.ToString() + " " + str;
sw = File.AppendText("log/" + year + "/" + month + "/RFlog.txt");
sw.WriteLine(str2);
sw.Close();
}
else
{
sw = File.CreateText("log/" + year + "/" + month + "/RFlog.txt");

string str1 = DateTime.Now.ToString() + " " + str;
sw.WriteLine(str1);
sw.Close();
}
}
}
catch (Exception e)
{

}



}




private void check_status()
{
try
{
SqlConnection con = new SqlConnection(arr1);

SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = "EXEC TAG_ZONE_STATUS_DEMO";
SqlDataAdapter da = new SqlDataAdapter(com);
DataTable dt = new DataTable();
da.Fill(dt);

SqlCommand com1 = new SqlCommand();
com1.Connection = con;
com1.CommandText = "select * from temp_tbl";
SqlDataAdapter da1 = new SqlDataAdapter(com1);
DataSet ds = new DataSet();
da1.Fill(ds);

if (ds.Tables[0].Rows[0][0].ToString() == "ALLOW")
{
if (p1 == p_type)
{
SqlCommand com2 = new SqlCommand();
com2.Connection = con;
com2.CommandText = "UPDATE tbl_tag_reader_master SET FLAG = 1 WHERE TAG_ID = '" + ds.Tables[0].Rows[0]["TAG_ID"] + "'";
con.Open();
com2.ExecuteNonQuery();
con.Close();
api.SetOutPort(0, 0);
Thread.Sleep(2000);

api.SetOutPort(0, 1);
}

}
}
catch (Exception ex)
{
logfile(ex.Message.ToString());
int i = 0;
while (i == 0)
{
i = connectReader();
// Thread.Sleep(2000);

}
logfile("Reader Connected.Out Cmd");
timer1.Start();

}
}

private void timer1_Tick(object sender, EventArgs e)
{


try
{

intcount = intcount + 1;
api.ClearIdBuf();
int status;
byte tag_cnt = 0;
int i, j;
byte[,] isobuf = new byte[100, 12];
string s = "";
string s1 = "";
string[] s2 = null;
string s3;
byte tagflag = 0;
DateTime dt;
dt = DateTime.Now;
string dt1 = dt.ToString("yyyy-MM-dd HH:mm:ss");
status = api.EpcMultiTagIdentify(ref isobuf, ref tag_cnt, ref tagflag);
if (status != 0)
{
DllNotFoundException dl = new DllNotFoundException();
throw dl;


}
else if (tag_cnt > 0)
{
for (i = 0; i < tag_cnt; i++)
{
s1 = string.Format("NO.{0:D}: ", TagCnt);
for (j = 0; j < 12; j++)
{
s = string.Format("{0:X2}", isobuf[i, j]);
s1 += s;
}
s2 = s1.Split(':');
s3 = DateTime.Now.ToString();
SqlConnection con1 = new SqlConnection(arr1);
SqlCommand com1 = new SqlCommand();
com1.Connection = con1;
com1.CommandText = "select count(*) from TBL_VEHICLE_OWNER_PERMISSION_MASTER where TAG_UID='" + s2[1].Trim() + "'";
SqlDataAdapter da23 = new SqlDataAdapter(com1);
DataTable dt23 = new DataTable();
da23.Fill(dt23);
int c23 = Int32.Parse(dt23.Rows[0][0].ToString());
if (c23 > 0)
{
SqlCommand com2 = new SqlCommand();
com2.Connection = con1;
com2.CommandText = "select PARKING_TYPE from TBL_VEHICLE_OWNER_PERMISSION_MASTER where TAG_UID='" + s2[1].Trim() + "'";
SqlDataAdapter da24 = new SqlDataAdapter(com2);
DataTable dt24 = new DataTable();
da24.Fill(dt24);
p_type = dt24.Rows[0][0].ToString();

}
else
{
p_type = "";
}




SqlCommand com;

com = new SqlCommand();
com.Connection = con1;
com.CommandText = "select * from tbl_tag_reader_master where tag_id='" + s2[1].Trim() + "' and datediff(second,timetag,'" + dt1 + "')<8";
DataTable dt12 = new DataTable();
SqlDataAdapter da12 = new SqlDataAdapter(com);
da12.Fill(dt12);
if (dt12.Rows.Count > 0)
{
}
else
{
com = new SqlCommand();
com.Connection = con1;
com.CommandText = "insert into tbl_tag_reader_master values('" + ip + "','" + s2[1].Trim() + "','" + System.DateTime.Now.ToString() + "','" + null + "','" + null + "','" + null + "','" + null + "','" + null + "','" + null + "')";
con1.Open();
com.ExecuteNonQuery();
con1.Close();
check_status();
TagCnt++;
}





}
}
if (intcount > 1800)
{
timercount2();
intcount = 0;
}
timer1.Start();
}
catch (DllNotFoundException d1)
{
logfile("Connection Broken. Reattempting Connection...");
int i = 0;
while (i == 0)
{
i = connectReader();
// Thread.Sleep(2000);

}
logfile("Reader Connected.Timer");
timer1.Start();
}
catch (Exception e1)
{
logfile(e1.Message.ToString());
int i = 0;
while (i == 0)
{
i = connectReader();
// Thread.Sleep(2000);

}
logfile("Reader Connected.Timer");

}
}

private void timer2_Tick(object sender, EventArgs e)
{

}
private void timercount2()
{
try
{
api.TcpCloseConnect();
int i = 0;
while (i == 0)
{
i = connectReader();
//Thread.Sleep(2000);

}
logfile("Reader Connected.Timer2");
}
catch (Exception e1)
{
logfile(e1.Message.ToString());
int i = 0;
while (i == 0)
{
i = connectReader();
//Thread.Sleep(2000);

}
logfile("Reader Connected.Timer2");
}
}
}
}












 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.