0

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");
        };
Phong Nguyễn
  • 790
  • 2
  • 5
  • 20
  • Basically, you can not update or do something in the client side like that. Checkout this one to get the ideas https://stackoverflow.com/questions/22592858/sqldependency-issue-with-the-asp-net – Edward N Nov 14 '17 at 09:46
  • Thanks @EdwardN, after your suggest. I was think about SignalR and I received this video help me a lot: https://www.youtube.com/watch?v=30m-7wpmbrc – Phong Nguyễn Nov 15 '17 at 05:58

0 Answers0