I had a hunch, that the Main Thread of the ASP Code Behind will switch to other thread when SqlDependency OnChange event occur. That why I can't call alert from Jquery or update the GUI. Please review below code and give me a solution for back to Main Thread.
Here is the code:
ASP Codebehind C#
protected void Page_Load(object sender, EventArgs e)
{
GetName();
}
void GetName()
{
ScriptManager.RegisterStartupScript(
this, this.GetType(), "Confirm123", "Confirm();", true);//Can exec in here
SqlDependency.Stop(stringconn);
SqlDependency.Start(stringconn);
SqlConnection sqlConnection = new SqlConnection(stringconn);
SqlCommand sqlCommand = new SqlCommand(cmd, sqlConnection);
SqlDependency dependency = new SqlDependency(sqlCommand);
if (sqlConnection.State == ConnectionState.Closed)
sqlConnection.Open();
dependency.OnChange += ((sender, e) => Dependency_OnChange(sender, e));
int reader = sqlCommand.ExecuteNonQuery();
}
public void Dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
SqlDependency dependency = sender as SqlDependency;
dependency.OnChange -= new OnChangeEventHandler(Dependency_OnChange);
ScriptManager.RegisterStartupScript(
this, this.GetType(), "Confirm123", "Confirm();", true);//Can't exec in here
GetName();
}
Javascript code
function Confirm() {
alert("Changed Detect");
};