在SqlDependency OnChange发生后,我无法更新我的UI

时间:2017-11-14 09:40:07

标签: c# sql asp.net sqldependency

我有一种预感,当SqlDependency OnChange事件发生时,ASP Code Behind的主线程将切换到其他线程。这就是我无法从Jquery调用警报或更新GUI的原因。请查看下面的代码并给我一个回到主线程的解决方案。

以下是代码:

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代码

function Confirm() {
            alert("Changed Detect");
        };

0 个答案:

没有答案