I have written a windows service for one of our local servers. This service works like a gem on my local machine, does what it's supposed to (ACCP to exchange db data), but I'm not overly familiar with dependencies. Is this an appropriate way to receive/send a message?
string DBP3_US = DBP3_US;
string PING_DEPENDENCY = "SELECT [SomeColumn] FROM [SomeTable];";
protected override void OnStart(string[] args)
{
SqlDependency.Start(DBP3_US);
Thread Ping_US = new Thread(PingThread);
Ping_US.Name = "ping_US";
Ping_US.Start();
}
private void PingThread()
{
CreateCommandWithDependency(PING_DEPENDENCY, Ping_OnChange);
}
private void CreateCommandWithDependency(string queryText, OnChangeEventHandler e, string db = DBP3_US)
{
using (SqlConnection con = new SqlConnection(db))
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);
con.Open();
cmd.ExecuteNonQuery();
}
}
private void Ping_OnChange(object sender, SqlNotificationEventArgs e)
{
PingDependency();
Thread Ping_US = new Thread(PingThread);
Ping_US.Start();
}
private void PingDependency(string db = DBP3_US)
{
// Do whatever operations are required.
}