I understand that I need to constantly renew the OnChangeEventHandler every time a callback is received.
I would like to ensure that there is no existing handler before adding a new one.
How do I do this? Is there a count? (see HERE comment)?
public void InitialiseDependency(Action onDependencyMethod)
{
this.onDependencyMethod = onDependencyMethod;
SqlDependency.Start(connectionString, null);
using (SqlCommand command = new SqlCommand(
"SELECT [Symbol] FROM [dbo].[tblOrders] WHERE [Status] = 'NEW'",
conn))
{
SqlDependency dependency = new SqlDependency(command);
//HERE if (dependency.OnChangeEventHandlers.count()) > 0 { return; }
dependency.OnChange += new OnChangeEventHandler(OnDependencyChange);
using (SqlDataReader reader = command.ExecuteReader())
{
// Process the DataReader.
}
}
}