Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

##Interface

Interface

##Code

Code

##Interface

##Code

Interface

Code

Rollback to Revision 3
Source Link
rolfl
  • 98.1k
  • 17
  • 219
  • 419

This code works perfectly, but does it make sense and can it be optimized further? I have posted my code reflecting some of the suggestions made here.

#region Declarations
private bool isPermitted()
{
 try
 {
 SqlClientPermission clientPermission = new SqlClientPermission(PermissionState.Unrestricted);
 clientPermission.Demand();
 return true;
 }
 catch
 {
 return false;
 lbxSystemAvailabilityReply.Items.Add("Failed permissions test.");
 }
}
string db = Connect.ConnectGetDBString("""DatabaseName");
static int RecordsProcessed = 0;
static decimal MaxRecordsRetained = 10;
const string SYSTEM_AVAILABILITY_QUERY = "SELECT [Columns] FROM [dbo].[System];";
const string SYSTEM_AVAILABILITY_REPLY_QUERY = "SELECT [Columns] FROM [dbo].[SystemReply];";
const string SYSTEM_AVAILABILITY_AWAIT = "SELECT [Columns] FROM [dbo].[System] WHERE [Columns] = 0;";
#endregion
public SystemAvailabilityLog()
{
 InitializeComponent();
}
private void TransactionLog_LoaddbTransactionLog_Load(object sender, System.EventArgs e)
{
 SqlDependency.Stop(db);
 SqlDependency.Start(db);
 Thread sysAvail = new Thread(SystemAvailabilityThread);
 Thread sysAvailRep = new Thread(SystemAvailabilityReplyThread);
 
 sysAvail.Start();
 sysAvailRep.Start();
 txtLastRestart.Text = DateTime.Now.ToString();
}
private void TransactionLog_FormClosingdbTransactionLog_FormClosing(object sender, FormClosingEventArgs e)
{
 SqlDependency.Stop(db);
}
#region Methods and Functions
private SqlCommand CreateCommandWithDependency(string queryText, SqlConnection con, OnChangeEventHandler e)
{
 using (SqlCommand cmd = con.CreateCommand())
 {
 cmd.CommandText = queryText;
 cmd.CommandType = CommandType.Text;
 cmd.Notification = null;
 SqlDependency sqlDep = new SqlDependency(cmd);
 sqlDep.OnChange += new OnChangeEventHandler(e);

 return cmd;
 }
}
private SqlCommand NonQueryCommand(string queryText, SqlConnection con, params SqlParameter[] param)
{
 using (SqlCommand cmd = con.CreateCommand())
 {
 cmd.CommandText = queryText;
 cmd.CommandType = CommandType.StoredProcedure;
 for (int i = 0; i < param.Count(); i++)
 {
 cmd.Parameters.Add(param[i]);
 }

 return cmd;
 }
}
private void UpdateRecordCount()
{
 RecordsProcessed++;
 if (txtProcessed.InvokeRequired)
 {
  txtProcessed.Invoke(new ThreadStart(delegate{txtProcessed.Text = RecordsProcessed.ToString();}));
 }
 else
 {
 txtProcessed.Text = RecordsProcessed.ToString();
 }
}
private void SendSystemAvailabilityPing()
{
 using (SqlConnection con = new SqlConnection(db))
 {
 SystemAvailability oPing = new SystemAvailability();
 oPing.PingDate = DateTime.Today.Date.ToString("MMddyyyy");
 oPing.PingTime = DateTime.Now.TimeOfDay.ToString().Replace(":", "").Remove(6);
 string xmlSend = oPing.GetXMLString;
 using (SqlDataReader dr = Connect.ExecuteReader("[db_InsertSystemAvailability]", con,
 new SqlParameter("@XmlSend", xmlSend))) { }
 }
}
private void SystemAvailabilityReply()
{
 try
 {
 if (!isPermitted())
 return;
 using (SqlConnection con = new SqlConnection(db))
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_AWAIT, con, SystemAvailabilityReply_OnChange))
 {
 con.Open();
 using (SqlDataReaderSqlConnection drcon = cmd.ExecuteReadernew SqlConnection(db))
 {
 ifusing (drSqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_AWAIT, con, SystemAvailabilityReply_OnChange))
 {
 con.HasRowsOpen();
 whileusing (SqlDataReader dr = cmd.ReadExecuteReader())
 {
 if (lbxSystemAvailabilityReply.Itemsdr.Count >= MaxRecordsRetainedHasRows)
 {
 while (dr.Read())
 {
 if (lbxSystemAvailabilityReply.Items.Count >= MaxRecordsRetained)
 {
 lbxSystemAvailabilityReply.Items.RemoveAt(0);
 }
 SystemAvailabilityReply sarPing = new SystemAvailabilityReply();
 sarPing.Param1PingID = dr["Param1"]dr["PingID"].ToString();
 sarPing.Param2 = dr["Param2"].ToString();
 using (SqlConnection conInsert = new SqlConnection(db))
 sarPing.Param3 = dr["Param3"].ToString();
 {
 using (SqlConnection conInsert = newusing SqlConnection(db))
SqlDataReader drInsert = Connect.ExecuteReader("[db_InsertSystemAvailabilityReply]", conInsert,
 using (SqlCommand cmdInsert = NonQueryCommand("[_InsertSystemAvailabilityReply]", conInsert,
 new SqlParameter("@Param1", sarPing.Param1PingID),))
 { }
 new}
 SqlParameter("@Param2", sarPing string transactionDetail = string.Param2)Format("PingID: {0} PingDate: {1} PingTime: {2}",
 new SqlParameter("@Param3" sarPing.PingID, sarPing.Param3))PingDate, sarPing.PingTime);
 { lbxSystemAvailabilityReply.Items.Add(transactionDetail);
 conInsert.Open UpdateRecordCount();
 cmdInsert.ExecuteNonQuery();}
 }
 
 string transactionDetail = string.Format("Param1: {0} Param2: {1} Param3: {2}",
 sarPing.Param1, sarPing.Param2, sarPing.Param3);
 lbxSystemAvailabilityReply.Items.Add(transactionDetail);
 UpdateRecordCount();
 }
 }
 }
 }
 }
 catch (Exception ex)
 {
 // There will be code here later after Error db is established.
 }
}
private void SystemAvailability()
{
 lbxSystemAvailability.Items.Clear();
 if (!isPermitted())
 return;{
 string pingDate = DateTime.Today.ToShortDateString();
 string pingTime = DateTime.Now.TimeOfDay.ToString();
 string pingMessage = string.Format("Ping Received: {0} @ {1}", pingDate, pingTime);
 lbxSystemAvailability.Items.Add(pingMessage);
 }
}
private void SystemAvailabilityThread()
{
 using (SqlConnection con = new SqlConnection(db))
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_QUERY, con, SystemAvailability_OnChange))
 {
 con.Open();
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_QUERY, con, SystemAvailability_OnChange))
 {
 using (SqlDataReader dr = cmd.ExecuteNonQueryExecuteReader();) { }
 }
 }
}
private void SystemAvailabilityReplyThread()
{
 using (SqlConnection con = new SqlConnection(db))
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_REPLY_QUERY, con, SystemAvailabilityReply_OnChange))
 {
 con.Open();
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_REPLY_QUERY, con, SystemAvailabilityReply_OnChange))
 {
 using (SqlDataReader dr = cmd.ExecuteNonQueryExecuteReader();) { }
 }
 }
}
#endregion
#region Object Handles
private void btnSystemAvailabilityForm_Click(object sender, EventArgs e)
{
 Thread sysPing = new Thread(SendSystemAvailabilityPing);
 sysPing.Start();
}
private void nudRecentTransactionCount_ValueChanged(object sender, EventArgs e)
{
 MaxRecordsRetained = nudRecentTransactionCount.Value;
 while (lbxSystemAvailabilityReply.Items.Count > MaxRecordsRetained)
 {
 lbxSystemAvailabilityReply.Items.RemoveAt(0);
 }
}
private void SystemAvailability_OnChange(object sender, SqlNotificationEventArgs e)
{
 if (this.InvokeRequired)
 {
 lbxSystemAvailabilityReply.BeginInvoke(new MethodInvoker(SystemAvailabilityReply));
 }
 else
 {
 SystemAvailabilityReply();
 }

 Thread sysAvail = new Thread(SystemAvailabilityThread);
 sysAvail.Start();
}
private void SystemAvailabilityReply_OnChange(object sender, SqlNotificationEventArgs e)
{
 if (this.InvokeRequired)
 {
 lbxSystemAvailability.BeginInvoke(new MethodInvoker(SystemAvailability));
 }
 else
 {
 SystemAvailability();
 }

 Thread sysAvailRep = new Thread(SystemAvailabilityReplyThread);
 sysAvailRep.Start();
}
#endregion

This code works perfectly, but does it make sense and can it be optimized further? I have posted my code reflecting some of the suggestions made here.

#region Declarations
private bool isPermitted()
{
 try
 {
 SqlClientPermission clientPermission = new SqlClientPermission(PermissionState.Unrestricted);
 clientPermission.Demand();
 return true;
 }
 catch
 {
 return false;
 lbxSystemAvailabilityReply.Items.Add("Failed permissions test.");
 }
}
string db = Connect.Connect("");
static int RecordsProcessed = 0;
static decimal MaxRecordsRetained = 10;
const string SYSTEM_AVAILABILITY_QUERY = "SELECT [Columns] FROM [dbo].[System];";
const string SYSTEM_AVAILABILITY_REPLY_QUERY = "SELECT [Columns] FROM [dbo].[SystemReply];";
const string SYSTEM_AVAILABILITY_AWAIT = "SELECT [Columns] FROM [dbo].[System] WHERE [Columns] = 0;";
#endregion
public SystemAvailabilityLog()
{
 InitializeComponent();
}
private void TransactionLog_Load(object sender, System.EventArgs e)
{
 SqlDependency.Stop(db);
 SqlDependency.Start(db);
 Thread sysAvail = new Thread(SystemAvailabilityThread);
 Thread sysAvailRep = new Thread(SystemAvailabilityReplyThread);
 
 sysAvail.Start();
 sysAvailRep.Start();
 txtLastRestart.Text = DateTime.Now.ToString();
}
private void TransactionLog_FormClosing(object sender, FormClosingEventArgs e)
{
 SqlDependency.Stop(db);
}
#region Methods and Functions
private SqlCommand CreateCommandWithDependency(string queryText, SqlConnection con, OnChangeEventHandler e)
{
 using (SqlCommand cmd = con.CreateCommand())
 {
 cmd.CommandText = queryText;
 cmd.CommandType = CommandType.Text;
 cmd.Notification = null;
 SqlDependency sqlDep = new SqlDependency(cmd);
 sqlDep.OnChange += new OnChangeEventHandler(e);

 return cmd;
 }
}
private SqlCommand NonQueryCommand(string queryText, SqlConnection con, params SqlParameter[] param)
{
 using (SqlCommand cmd = con.CreateCommand())
 {
 cmd.CommandText = queryText;
 cmd.CommandType = CommandType.StoredProcedure;
 for (int i = 0; i < param.Count(); i++)
 {
 cmd.Parameters.Add(param[i]);
 }

 return cmd;
 }
}
private void UpdateRecordCount()
{
 RecordsProcessed++;
 if (txtProcessed.InvokeRequired)
 txtProcessed.Invoke(new ThreadStart(delegate{txtProcessed.Text = RecordsProcessed.ToString();}));
 else
 txtProcessed.Text = RecordsProcessed.ToString();
}
private void SystemAvailabilityReply()
{
 try
 {
 if (!isPermitted())
 return;
 using (SqlConnection con = new SqlConnection(db))
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_AWAIT, con, SystemAvailabilityReply_OnChange))
 {
 con.Open();
 using (SqlDataReader dr = cmd.ExecuteReader())
 {
 if (dr.HasRows)
 while (dr.Read())
 {
 if (lbxSystemAvailabilityReply.Items.Count >= MaxRecordsRetained)
 {
 lbxSystemAvailabilityReply.Items.RemoveAt(0);
 }
 SystemAvailabilityReply sarPing = new SystemAvailabilityReply();
 sarPing.Param1 = dr["Param1"].ToString();
 sarPing.Param2 = dr["Param2"].ToString();
 sarPing.Param3 = dr["Param3"].ToString();
 using (SqlConnection conInsert = new SqlConnection(db))
 using (SqlCommand cmdInsert = NonQueryCommand("[_InsertSystemAvailabilityReply]", conInsert,
 new SqlParameter("@Param1", sarPing.Param1),
 new SqlParameter("@Param2", sarPing.Param2),
 new SqlParameter("@Param3", sarPing.Param3)))
 {
 conInsert.Open();
 cmdInsert.ExecuteNonQuery();
 }
 
 string transactionDetail = string.Format("Param1: {0} Param2: {1} Param3: {2}",
 sarPing.Param1, sarPing.Param2, sarPing.Param3);
 lbxSystemAvailabilityReply.Items.Add(transactionDetail);
 UpdateRecordCount();
 }
 }
 }
 }
 catch (Exception ex)
 {
 // There will be code here later after Error db is established.
 }
}
private void SystemAvailability()
{
 lbxSystemAvailability.Items.Clear();
 if (!isPermitted())
 return;
 string pingDate = DateTime.Today.ToShortDateString();
 string pingTime = DateTime.Now.TimeOfDay.ToString();
 string pingMessage = string.Format("Ping Received: {0} @ {1}", pingDate, pingTime);
 lbxSystemAvailability.Items.Add(pingMessage);
}
private void SystemAvailabilityThread()
{
 using (SqlConnection con = new SqlConnection(db))
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_QUERY, con, SystemAvailability_OnChange))
 {
 con.Open();
 cmd.ExecuteNonQuery();
 }
}
private void SystemAvailabilityReplyThread()
{
 using (SqlConnection con = new SqlConnection(db))
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_REPLY_QUERY, con, SystemAvailabilityReply_OnChange))
 {
 con.Open();
 cmd.ExecuteNonQuery();
 }
}
#endregion
#region Object Handles
private void nudRecentTransactionCount_ValueChanged(object sender, EventArgs e)
{
 MaxRecordsRetained = nudRecentTransactionCount.Value;
 while (lbxSystemAvailabilityReply.Items.Count > MaxRecordsRetained)
 {
 lbxSystemAvailabilityReply.Items.RemoveAt(0);
 }
}
private void SystemAvailability_OnChange(object sender, SqlNotificationEventArgs e)
{
 if (this.InvokeRequired)
 lbxSystemAvailabilityReply.BeginInvoke(new MethodInvoker(SystemAvailabilityReply));
 else
 SystemAvailabilityReply();
 Thread sysAvail = new Thread(SystemAvailabilityThread);
 sysAvail.Start();
}
private void SystemAvailabilityReply_OnChange(object sender, SqlNotificationEventArgs e)
{
 if (this.InvokeRequired)
 lbxSystemAvailability.BeginInvoke(new MethodInvoker(SystemAvailability));
 else
 SystemAvailability();
 Thread sysAvailRep = new Thread(SystemAvailabilityReplyThread);
 sysAvailRep.Start();
}
#endregion

This code works perfectly, but does it make sense and can it be optimized?

#region Declarations
private bool isPermitted()
{
 try
 {
 SqlClientPermission clientPermission = new SqlClientPermission(PermissionState.Unrestricted);
 clientPermission.Demand();
 return true;
 }
 catch
 {
 return false;
 lbxSystemAvailabilityReply.Items.Add("Failed permissions test.");
 }
}
string db = Connect.GetDBString("DatabaseName");
static int RecordsProcessed = 0;
static decimal MaxRecordsRetained = 10;
const string SYSTEM_AVAILABILITY_QUERY = "SELECT [Columns] FROM [dbo].[System];";
const string SYSTEM_AVAILABILITY_REPLY_QUERY = "SELECT [Columns] FROM [dbo].[SystemReply];";
const string SYSTEM_AVAILABILITY_AWAIT = "SELECT [Columns] FROM [dbo].[System] WHERE [Columns] = 0;";
#endregion
public SystemAvailabilityLog()
{
 InitializeComponent();
}
private void dbTransactionLog_Load(object sender, System.EventArgs e)
{
 SqlDependency.Stop(db);
 SqlDependency.Start(db);
 Thread sysAvail = new Thread(SystemAvailabilityThread);
 Thread sysAvailRep = new Thread(SystemAvailabilityReplyThread);
 
 sysAvail.Start();
 sysAvailRep.Start();
 txtLastRestart.Text = DateTime.Now.ToString();
}
private void dbTransactionLog_FormClosing(object sender, FormClosingEventArgs e)
{
 SqlDependency.Stop(db);
}
#region Methods and Functions
private SqlCommand CreateCommandWithDependency(string queryText, SqlConnection con, OnChangeEventHandler e)
{
 using (SqlCommand cmd = con.CreateCommand())
 {
 cmd.CommandText = queryText;
 cmd.CommandType = CommandType.Text;
 cmd.Notification = null;
 SqlDependency sqlDep = new SqlDependency(cmd);
 sqlDep.OnChange += new OnChangeEventHandler(e);
 return cmd;
 }
}
private void UpdateRecordCount()
{
 RecordsProcessed++;
 if (txtProcessed.InvokeRequired)
 {
  txtProcessed.Invoke(new ThreadStart(delegate{txtProcessed.Text = RecordsProcessed.ToString();}));
 }
 else
 {
 txtProcessed.Text = RecordsProcessed.ToString();
 }
}
private void SendSystemAvailabilityPing()
{
 using (SqlConnection con = new SqlConnection(db))
 {
 SystemAvailability oPing = new SystemAvailability();
 oPing.PingDate = DateTime.Today.Date.ToString("MMddyyyy");
 oPing.PingTime = DateTime.Now.TimeOfDay.ToString().Replace(":", "").Remove(6);
 string xmlSend = oPing.GetXMLString;
 using (SqlDataReader dr = Connect.ExecuteReader("[db_InsertSystemAvailability]", con,
 new SqlParameter("@XmlSend", xmlSend))) { }
 }
}
private void SystemAvailabilityReply()
{
 try
 {
 if (isPermitted())
 {
 using (SqlConnection con = new SqlConnection(db))
 {
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_AWAIT, con, SystemAvailabilityReply_OnChange))
 {
 con.Open();
 using (SqlDataReader dr = cmd.ExecuteReader())
 {
 if (dr.HasRows)
 {
 while (dr.Read())
 {
 if (lbxSystemAvailabilityReply.Items.Count >= MaxRecordsRetained)
 {
 lbxSystemAvailabilityReply.Items.RemoveAt(0);
 }
 SystemAvailabilityReply sarPing = new SystemAvailabilityReply();
 sarPing.PingID = dr["PingID"].ToString();
 using (SqlConnection conInsert = new SqlConnection(db))
 {
 using (SqlDataReader drInsert = Connect.ExecuteReader("[db_InsertSystemAvailabilityReply]", conInsert,
 new SqlParameter("@Param1", sarPing.PingID)))
 { }
 }
  string transactionDetail = string.Format("PingID: {0} PingDate: {1} PingTime: {2}",
  sarPing.PingID, sarPing.PingDate, sarPing.PingTime);
  lbxSystemAvailabilityReply.Items.Add(transactionDetail);
  UpdateRecordCount();
 }
 }
 }
 }
 }
 }
 }
 catch (Exception ex)
 {
 }
}
private void SystemAvailability()
{
 lbxSystemAvailability.Items.Clear();
 if (isPermitted())
 {
 string pingDate = DateTime.Today.ToShortDateString();
 string pingTime = DateTime.Now.TimeOfDay.ToString();
 string pingMessage = string.Format("Ping Received: {0} @ {1}", pingDate, pingTime);
 lbxSystemAvailability.Items.Add(pingMessage);
 }
}
private void SystemAvailabilityThread()
{
 using (SqlConnection con = new SqlConnection(db))
 {
 con.Open();
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_QUERY, con, SystemAvailability_OnChange))
 {
 using (SqlDataReader dr = cmd.ExecuteReader()) { }
 }
 }
}
private void SystemAvailabilityReplyThread()
{
 using (SqlConnection con = new SqlConnection(db))
 {
 con.Open();
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_REPLY_QUERY, con, SystemAvailabilityReply_OnChange))
 {
 using (SqlDataReader dr = cmd.ExecuteReader()) { }
 }
 }
}
#endregion
#region Object Handles
private void btnSystemAvailabilityForm_Click(object sender, EventArgs e)
{
 Thread sysPing = new Thread(SendSystemAvailabilityPing);
 sysPing.Start();
}
private void nudRecentTransactionCount_ValueChanged(object sender, EventArgs e)
{
 MaxRecordsRetained = nudRecentTransactionCount.Value;
 while (lbxSystemAvailabilityReply.Items.Count > MaxRecordsRetained)
 {
 lbxSystemAvailabilityReply.Items.RemoveAt(0);
 }
}
private void SystemAvailability_OnChange(object sender, SqlNotificationEventArgs e)
{
 if (this.InvokeRequired)
 {
 lbxSystemAvailabilityReply.BeginInvoke(new MethodInvoker(SystemAvailabilityReply));
 }
 else
 {
 SystemAvailabilityReply();
 }

 Thread sysAvail = new Thread(SystemAvailabilityThread);
 sysAvail.Start();
}
private void SystemAvailabilityReply_OnChange(object sender, SqlNotificationEventArgs e)
{
 if (this.InvokeRequired)
 {
 lbxSystemAvailability.BeginInvoke(new MethodInvoker(SystemAvailability));
 }
 else
 {
 SystemAvailability();
 }

 Thread sysAvailRep = new Thread(SystemAvailabilityReplyThread);
 sysAvailRep.Start();
}
#endregion
deleted 283 characters in body
Source Link
Volearix
  • 829
  • 3
  • 9
  • 22

This code works perfectly, but does it make sense and can it be optimized further? I have posted my code reflecting some of the suggestions made here.

#region Declarations
private bool isPermitted()
{
 try
 {
 SqlClientPermission clientPermission = new SqlClientPermission(PermissionState.Unrestricted);
 clientPermission.Demand();
 return true;
 }
 catch
 {
 return false;
 lbxSystemAvailabilityReply.Items.Add("Failed permissions test.");
 }
}
string db = Connect.GetDBStringConnect("DatabaseName""");
static int RecordsProcessed = 0;
static decimal MaxRecordsRetained = 10;
const string SYSTEM_AVAILABILITY_QUERY = "SELECT [Columns] FROM [dbo].[System];";
const string SYSTEM_AVAILABILITY_REPLY_QUERY = "SELECT [Columns] FROM [dbo].[SystemReply];";
const string SYSTEM_AVAILABILITY_AWAIT = "SELECT [Columns] FROM [dbo].[System] WHERE [Columns] = 0;";
#endregion
public SystemAvailabilityLog()
{
 InitializeComponent();
}
private void dbTransactionLog_LoadTransactionLog_Load(object sender, System.EventArgs e)
{
 SqlDependency.Stop(db);
 SqlDependency.Start(db);
 Thread sysAvail = new Thread(SystemAvailabilityThread);
 Thread sysAvailRep = new Thread(SystemAvailabilityReplyThread);
 
 sysAvail.Start();
 sysAvailRep.Start();
 txtLastRestart.Text = DateTime.Now.ToString();
}
private void dbTransactionLog_FormClosingTransactionLog_FormClosing(object sender, FormClosingEventArgs e)
{
 SqlDependency.Stop(db);
}
#region Methods and Functions
private SqlCommand CreateCommandWithDependency(string queryText, SqlConnection con, OnChangeEventHandler e)
{
 using (SqlCommand cmd = con.CreateCommand())
 {
 cmd.CommandText = queryText;
 cmd.CommandType = CommandType.Text;
 cmd.Notification = null;
 SqlDependency sqlDep = new SqlDependency(cmd);
 sqlDep.OnChange += new OnChangeEventHandler(e);

 return cmd;
 }
}
private SqlCommand NonQueryCommand(string queryText, SqlConnection con, params SqlParameter[] param)
{
 using (SqlCommand cmd = con.CreateCommand())
 {
 cmd.CommandText = queryText;
 cmd.CommandType = CommandType.StoredProcedure;
 for (int i = 0; i < param.Count(); i++)
 {
 cmd.Parameters.Add(param[i]);
 }

 return cmd;
 }
}
private void UpdateRecordCount()
{
 RecordsProcessed++;
 if (txtProcessed.InvokeRequired)
 {
  txtProcessed.Invoke(new ThreadStart(delegate{txtProcessed.Text = RecordsProcessed.ToString();}));
 }
 else
 {
 txtProcessed.Text = RecordsProcessed.ToString();
 }
}
private void SendSystemAvailabilityPing()
{
 using (SqlConnection con = new SqlConnection(db))
 {
 SystemAvailability oPing = new SystemAvailability();
 oPing.PingDate = DateTime.Today.Date.ToString("MMddyyyy");
 oPing.PingTime = DateTime.Now.TimeOfDay.ToString().Replace(":", "").Remove(6);
 string xmlSend = oPing.GetXMLString;
 using (SqlDataReader dr = Connect.ExecuteReader("[db_InsertSystemAvailability]", con,
 new SqlParameter("@XmlSend", xmlSend))) { }
 }
}
private void SystemAvailabilityReply()
{
 try
 {
 if (!isPermitted())
 return;
 using (SqlConnection con = new SqlConnection(db))
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_AWAIT, con, SystemAvailabilityReply_OnChange))
 {
 con.Open();
 using (SqlConnectionSqlDataReader condr = new SqlConnectioncmd.ExecuteReader(db))
 {
 using (SqlCommand cmd =if CreateCommandWithDependency(SYSTEM_AVAILABILITY_AWAIT, con, SystemAvailabilityReply_OnChange))
 {
 condr.Open(HasRows);
 usingwhile (SqlDataReader dr = cmd.ExecuteReaderRead())
 {
 if (drlbxSystemAvailabilityReply.HasRowsItems.Count >= MaxRecordsRetained)
 {
 while (dr.Read())
 {
 if (lbxSystemAvailabilityReply.Items.Count >= MaxRecordsRetained)
 {
 lbxSystemAvailabilityReply.Items.RemoveAt(0);
 }
 SystemAvailabilityReply sarPing = new SystemAvailabilityReply();
 sarPing.PingID = dr["PingID"].ToString();
 SystemAvailabilityReply sarPing = new SystemAvailabilityReply();
 using (SqlConnection conInsert = new SqlConnection(db))
 sarPing.Param1 = dr["Param1"].ToString();
 {
 sarPing.Param2 = dr["Param2"].ToString();
 using (SqlDataReader drInsertsarPing.Param3 = Connectdr["Param3"].ExecuteReaderToString("[db_InsertSystemAvailabilityReply]", conInsert,);
 using (SqlConnection conInsert = new SqlConnection(db))
 using (SqlCommand cmdInsert = NonQueryCommand("[_InsertSystemAvailabilityReply]", conInsert,
 new SqlParameter("@Param1", sarPing.PingID)))
 { }
 new SqlParameter("@Param1", sarPing.Param1),
 }
 string transactionDetail = string.Format("PingID: {0} PingDate: {1} PingTime: {2}",
 new SqlParameter("@Param2", sarPing.Param2),
 sarPing.PingID, sarPing.PingDate, sarPing.PingTime);
 new SqlParameter("@Param3", sarPing.Param3)))
 lbxSystemAvailabilityReply.Items.Add(transactionDetail);
 {
 UpdateRecordCount conInsert.Open();
 }cmdInsert.ExecuteNonQuery();
 }
 
 string transactionDetail = string.Format("Param1: {0} Param2: {1} Param3: {2}",
 sarPing.Param1, sarPing.Param2, sarPing.Param3);
 lbxSystemAvailabilityReply.Items.Add(transactionDetail);
 UpdateRecordCount();
 }
 }
 }
 }
 }
 catch (Exception ex)
 {
 // There will be code here later after Error db is established.
 }
}
private void SystemAvailability()
{
 lbxSystemAvailability.Items.Clear();
 if (!isPermitted())
 { return;
 string pingDate = DateTime.Today.ToShortDateString();
 string pingTime = DateTime.Now.TimeOfDay.ToString();
 string pingMessage = string.Format("Ping Received: {0} @ {1}", pingDate, pingTime);
 lbxSystemAvailability.Items.Add(pingMessage);
 }
}
private void SystemAvailabilityThread()
{
 using (SqlConnection con = new SqlConnection(db))
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_QUERY, con, SystemAvailability_OnChange))
 {
 con.Open();
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_QUERY, con, SystemAvailability_OnChange))
 {
 using (SqlDataReader dr = cmd.ExecuteReaderExecuteNonQuery()) { }
 };
 }
}
private void SystemAvailabilityReplyThread()
{
 using (SqlConnection con = new SqlConnection(db))
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_REPLY_QUERY, con, SystemAvailabilityReply_OnChange))
 {
 con.Open();
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_REPLY_QUERY, con, SystemAvailabilityReply_OnChange))
 {
 using (SqlDataReader dr = cmd.ExecuteReaderExecuteNonQuery()) { }
 };
 }
}
#endregion
#region Object Handles
private void btnSystemAvailabilityForm_Click(object sender, EventArgs e)
{
 Thread sysPing = new Thread(SendSystemAvailabilityPing);
 sysPing.Start();
}
private void nudRecentTransactionCount_ValueChanged(object sender, EventArgs e)
{
 MaxRecordsRetained = nudRecentTransactionCount.Value;
 while (lbxSystemAvailabilityReply.Items.Count > MaxRecordsRetained)
 {
 lbxSystemAvailabilityReply.Items.RemoveAt(0);
 }
}
private void SystemAvailability_OnChange(object sender, SqlNotificationEventArgs e)
{
 if (this.InvokeRequired)
 {
 lbxSystemAvailabilityReply.BeginInvoke(new MethodInvoker(SystemAvailabilityReply));
 }
 else
 {
 SystemAvailabilityReply();
 }

 Thread sysAvail = new Thread(SystemAvailabilityThread);
 sysAvail.Start();
}
private void SystemAvailabilityReply_OnChange(object sender, SqlNotificationEventArgs e)
{
 if (this.InvokeRequired)
 {
 lbxSystemAvailability.BeginInvoke(new MethodInvoker(SystemAvailability));
 }
 else
 {
 SystemAvailability();
 }

 Thread sysAvailRep = new Thread(SystemAvailabilityReplyThread);
 sysAvailRep.Start();
}
#endregion

This code works perfectly, but does it make sense and can it be optimized?

#region Declarations
private bool isPermitted()
{
 try
 {
 SqlClientPermission clientPermission = new SqlClientPermission(PermissionState.Unrestricted);
 clientPermission.Demand();
 return true;
 }
 catch
 {
 return false;
 lbxSystemAvailabilityReply.Items.Add("Failed permissions test.");
 }
}
string db = Connect.GetDBString("DatabaseName");
static int RecordsProcessed = 0;
static decimal MaxRecordsRetained = 10;
const string SYSTEM_AVAILABILITY_QUERY = "SELECT [Columns] FROM [dbo].[System];";
const string SYSTEM_AVAILABILITY_REPLY_QUERY = "SELECT [Columns] FROM [dbo].[SystemReply];";
const string SYSTEM_AVAILABILITY_AWAIT = "SELECT [Columns] FROM [dbo].[System] WHERE [Columns] = 0;";
#endregion
public SystemAvailabilityLog()
{
 InitializeComponent();
}
private void dbTransactionLog_Load(object sender, System.EventArgs e)
{
 SqlDependency.Stop(db);
 SqlDependency.Start(db);
 Thread sysAvail = new Thread(SystemAvailabilityThread);
 Thread sysAvailRep = new Thread(SystemAvailabilityReplyThread);
 
 sysAvail.Start();
 sysAvailRep.Start();
 txtLastRestart.Text = DateTime.Now.ToString();
}
private void dbTransactionLog_FormClosing(object sender, FormClosingEventArgs e)
{
 SqlDependency.Stop(db);
}
#region Methods and Functions
private SqlCommand CreateCommandWithDependency(string queryText, SqlConnection con, OnChangeEventHandler e)
{
 using (SqlCommand cmd = con.CreateCommand())
 {
 cmd.CommandText = queryText;
 cmd.CommandType = CommandType.Text;
 cmd.Notification = null;
 SqlDependency sqlDep = new SqlDependency(cmd);
 sqlDep.OnChange += new OnChangeEventHandler(e);
 return cmd;
 }
}
private void UpdateRecordCount()
{
 RecordsProcessed++;
 if (txtProcessed.InvokeRequired)
 {
  txtProcessed.Invoke(new ThreadStart(delegate{txtProcessed.Text = RecordsProcessed.ToString();}));
 }
 else
 {
 txtProcessed.Text = RecordsProcessed.ToString();
 }
}
private void SendSystemAvailabilityPing()
{
 using (SqlConnection con = new SqlConnection(db))
 {
 SystemAvailability oPing = new SystemAvailability();
 oPing.PingDate = DateTime.Today.Date.ToString("MMddyyyy");
 oPing.PingTime = DateTime.Now.TimeOfDay.ToString().Replace(":", "").Remove(6);
 string xmlSend = oPing.GetXMLString;
 using (SqlDataReader dr = Connect.ExecuteReader("[db_InsertSystemAvailability]", con,
 new SqlParameter("@XmlSend", xmlSend))) { }
 }
}
private void SystemAvailabilityReply()
{
 try
 {
 if (isPermitted())
 {
 using (SqlConnection con = new SqlConnection(db))
 {
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_AWAIT, con, SystemAvailabilityReply_OnChange))
 {
 con.Open();
 using (SqlDataReader dr = cmd.ExecuteReader())
 {
 if (dr.HasRows)
 {
 while (dr.Read())
 {
 if (lbxSystemAvailabilityReply.Items.Count >= MaxRecordsRetained)
 {
 lbxSystemAvailabilityReply.Items.RemoveAt(0);
 }
 SystemAvailabilityReply sarPing = new SystemAvailabilityReply();
 sarPing.PingID = dr["PingID"].ToString();
 using (SqlConnection conInsert = new SqlConnection(db))
 {
 using (SqlDataReader drInsert = Connect.ExecuteReader("[db_InsertSystemAvailabilityReply]", conInsert,
 new SqlParameter("@Param1", sarPing.PingID)))
 { }
 }
 string transactionDetail = string.Format("PingID: {0} PingDate: {1} PingTime: {2}",
 sarPing.PingID, sarPing.PingDate, sarPing.PingTime);
 lbxSystemAvailabilityReply.Items.Add(transactionDetail);
 UpdateRecordCount();
 }
 }
 }
 }
 }
 }
 }
 catch (Exception ex)
 {
 }
}
private void SystemAvailability()
{
 lbxSystemAvailability.Items.Clear();
 if (isPermitted())
 {
 string pingDate = DateTime.Today.ToShortDateString();
 string pingTime = DateTime.Now.TimeOfDay.ToString();
 string pingMessage = string.Format("Ping Received: {0} @ {1}", pingDate, pingTime);
 lbxSystemAvailability.Items.Add(pingMessage);
 }
}
private void SystemAvailabilityThread()
{
 using (SqlConnection con = new SqlConnection(db))
 {
 con.Open();
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_QUERY, con, SystemAvailability_OnChange))
 {
 using (SqlDataReader dr = cmd.ExecuteReader()) { }
 }
 }
}
private void SystemAvailabilityReplyThread()
{
 using (SqlConnection con = new SqlConnection(db))
 {
 con.Open();
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_REPLY_QUERY, con, SystemAvailabilityReply_OnChange))
 {
 using (SqlDataReader dr = cmd.ExecuteReader()) { }
 }
 }
}
#endregion
#region Object Handles
private void btnSystemAvailabilityForm_Click(object sender, EventArgs e)
{
 Thread sysPing = new Thread(SendSystemAvailabilityPing);
 sysPing.Start();
}
private void nudRecentTransactionCount_ValueChanged(object sender, EventArgs e)
{
 MaxRecordsRetained = nudRecentTransactionCount.Value;
 while (lbxSystemAvailabilityReply.Items.Count > MaxRecordsRetained)
 {
 lbxSystemAvailabilityReply.Items.RemoveAt(0);
 }
}
private void SystemAvailability_OnChange(object sender, SqlNotificationEventArgs e)
{
 if (this.InvokeRequired)
 {
 lbxSystemAvailabilityReply.BeginInvoke(new MethodInvoker(SystemAvailabilityReply));
 }
 else
 {
 SystemAvailabilityReply();
 }

 Thread sysAvail = new Thread(SystemAvailabilityThread);
 sysAvail.Start();
}
private void SystemAvailabilityReply_OnChange(object sender, SqlNotificationEventArgs e)
{
 if (this.InvokeRequired)
 {
 lbxSystemAvailability.BeginInvoke(new MethodInvoker(SystemAvailability));
 }
 else
 {
 SystemAvailability();
 }

 Thread sysAvailRep = new Thread(SystemAvailabilityReplyThread);
 sysAvailRep.Start();
}
#endregion

This code works perfectly, but does it make sense and can it be optimized further? I have posted my code reflecting some of the suggestions made here.

#region Declarations
private bool isPermitted()
{
 try
 {
 SqlClientPermission clientPermission = new SqlClientPermission(PermissionState.Unrestricted);
 clientPermission.Demand();
 return true;
 }
 catch
 {
 return false;
 lbxSystemAvailabilityReply.Items.Add("Failed permissions test.");
 }
}
string db = Connect.Connect("");
static int RecordsProcessed = 0;
static decimal MaxRecordsRetained = 10;
const string SYSTEM_AVAILABILITY_QUERY = "SELECT [Columns] FROM [dbo].[System];";
const string SYSTEM_AVAILABILITY_REPLY_QUERY = "SELECT [Columns] FROM [dbo].[SystemReply];";
const string SYSTEM_AVAILABILITY_AWAIT = "SELECT [Columns] FROM [dbo].[System] WHERE [Columns] = 0;";
#endregion
public SystemAvailabilityLog()
{
 InitializeComponent();
}
private void TransactionLog_Load(object sender, System.EventArgs e)
{
 SqlDependency.Stop(db);
 SqlDependency.Start(db);
 Thread sysAvail = new Thread(SystemAvailabilityThread);
 Thread sysAvailRep = new Thread(SystemAvailabilityReplyThread);
 
 sysAvail.Start();
 sysAvailRep.Start();
 txtLastRestart.Text = DateTime.Now.ToString();
}
private void TransactionLog_FormClosing(object sender, FormClosingEventArgs e)
{
 SqlDependency.Stop(db);
}
#region Methods and Functions
private SqlCommand CreateCommandWithDependency(string queryText, SqlConnection con, OnChangeEventHandler e)
{
 using (SqlCommand cmd = con.CreateCommand())
 {
 cmd.CommandText = queryText;
 cmd.CommandType = CommandType.Text;
 cmd.Notification = null;
 SqlDependency sqlDep = new SqlDependency(cmd);
 sqlDep.OnChange += new OnChangeEventHandler(e);

 return cmd;
 }
}
private SqlCommand NonQueryCommand(string queryText, SqlConnection con, params SqlParameter[] param)
{
 using (SqlCommand cmd = con.CreateCommand())
 {
 cmd.CommandText = queryText;
 cmd.CommandType = CommandType.StoredProcedure;
 for (int i = 0; i < param.Count(); i++)
 {
 cmd.Parameters.Add(param[i]);
 }

 return cmd;
 }
}
private void UpdateRecordCount()
{
 RecordsProcessed++;
 if (txtProcessed.InvokeRequired)
 txtProcessed.Invoke(new ThreadStart(delegate{txtProcessed.Text = RecordsProcessed.ToString();}));
 else
 txtProcessed.Text = RecordsProcessed.ToString();
}
private void SystemAvailabilityReply()
{
 try
 {
 if (!isPermitted())
 return;
 using (SqlConnection con = new SqlConnection(db))
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_AWAIT, con, SystemAvailabilityReply_OnChange))
 {
 con.Open();
 using (SqlDataReader dr = cmd.ExecuteReader())
 {
 if (dr.HasRows)
 while (dr.Read())
 {
 if (lbxSystemAvailabilityReply.Items.Count >= MaxRecordsRetained)
 {
 lbxSystemAvailabilityReply.Items.RemoveAt(0);
 }
 SystemAvailabilityReply sarPing = new SystemAvailabilityReply();
 sarPing.Param1 = dr["Param1"].ToString();
 sarPing.Param2 = dr["Param2"].ToString();
 sarPing.Param3 = dr["Param3"].ToString();
 using (SqlConnection conInsert = new SqlConnection(db))
 using (SqlCommand cmdInsert = NonQueryCommand("[_InsertSystemAvailabilityReply]", conInsert,
 new SqlParameter("@Param1", sarPing.Param1),
 new SqlParameter("@Param2", sarPing.Param2),
 new SqlParameter("@Param3", sarPing.Param3)))
 {
  conInsert.Open();
 cmdInsert.ExecuteNonQuery();
 }
 
 string transactionDetail = string.Format("Param1: {0} Param2: {1} Param3: {2}",
 sarPing.Param1, sarPing.Param2, sarPing.Param3);
 lbxSystemAvailabilityReply.Items.Add(transactionDetail);
 UpdateRecordCount();
 }
 }
 }
 }
 catch (Exception ex)
 {
 // There will be code here later after Error db is established.
 }
}
private void SystemAvailability()
{
 lbxSystemAvailability.Items.Clear();
 if (!isPermitted())
  return;
 string pingDate = DateTime.Today.ToShortDateString();
 string pingTime = DateTime.Now.TimeOfDay.ToString();
 string pingMessage = string.Format("Ping Received: {0} @ {1}", pingDate, pingTime);
 lbxSystemAvailability.Items.Add(pingMessage);
}
private void SystemAvailabilityThread()
{
 using (SqlConnection con = new SqlConnection(db))
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_QUERY, con, SystemAvailability_OnChange))
 {
 con.Open();
 cmd.ExecuteNonQuery();
 }
}
private void SystemAvailabilityReplyThread()
{
 using (SqlConnection con = new SqlConnection(db))
 using (SqlCommand cmd = CreateCommandWithDependency(SYSTEM_AVAILABILITY_REPLY_QUERY, con, SystemAvailabilityReply_OnChange))
 {
 con.Open();
 cmd.ExecuteNonQuery();
 }
}
#endregion
#region Object Handles
private void nudRecentTransactionCount_ValueChanged(object sender, EventArgs e)
{
 MaxRecordsRetained = nudRecentTransactionCount.Value;
 while (lbxSystemAvailabilityReply.Items.Count > MaxRecordsRetained)
 {
 lbxSystemAvailabilityReply.Items.RemoveAt(0);
 }
}
private void SystemAvailability_OnChange(object sender, SqlNotificationEventArgs e)
{
 if (this.InvokeRequired)
 lbxSystemAvailabilityReply.BeginInvoke(new MethodInvoker(SystemAvailabilityReply));
 else
 SystemAvailabilityReply();
 Thread sysAvail = new Thread(SystemAvailabilityThread);
 sysAvail.Start();
}
private void SystemAvailabilityReply_OnChange(object sender, SqlNotificationEventArgs e)
{
 if (this.InvokeRequired)
 lbxSystemAvailability.BeginInvoke(new MethodInvoker(SystemAvailability));
 else
 SystemAvailability();
 Thread sysAvailRep = new Thread(SystemAvailabilityReplyThread);
 sysAvailRep.Start();
}
#endregion
added 7 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Improved title and text
Source Link
Phrancis
  • 20.5k
  • 6
  • 69
  • 155
Loading
Source Link
Volearix
  • 829
  • 3
  • 9
  • 22
Loading
default

AltStyle によって変換されたページ (->オリジナル) /