Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It does spawn in a different thread!! You can create a simple Windows Application to test this, and you'll see how the OnChange handler can't modify any UI controls directly (you'd get something like "Cross-thread operation not valid: Control XXX accessed from a thread other than the thread it was created on"). To overcome this, you call BeginInvoke.</p> <p>Here's a simple Windows application to test SqlDependencies (I hope you can imagine the UI).</p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Linq; using System.Data.Linq.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace TestSqlDependancies { public partial class SqlDependencyTester : Form { SqlDependency sqlDepenency = null; SqlCommand command; SqlNotificationEventArgs msg; public Form1() { InitializeComponent(); } private void label1_Click(object sender, EventArgs e) { } public delegate void InvokeDelegate(); void sqlDepenency_OnChange(object sender, SqlNotificationEventArgs e) { msg = e; this.BeginInvoke(new InvokeDelegate(Notify)); } private void Notify() { listBox1.Items.Add(DateTime.Now.ToString("HH:mm:ss:fff") + " - Notfication received. SqlDependency " + (sqlDepenency.HasChanges ? " has changes." : " reports no change. ") + msg.Type.ToString() + "-" + msg.Info.ToString()); } private void button1_Click(object sender, EventArgs e) { SetDependency(); } private void SetDependency() { try { using (TicketDataContext dc = new TicketDataContext()) { SqlDependency.Start(dc.Connection.ConnectionString); command = new SqlCommand(textBox1.Text, (SqlConnection)dc.Connection); sqlDepenency = new SqlDependency(command); sqlDepenency.OnChange += new OnChangeEventHandler(sqlDepenency_OnChange); command.Connection.Open(); command.ExecuteReader(); } } catch (Exception e) { listBox1.Items.Add(DateTime.Now.ToString("HH:mm:ss:fff") + e.Message); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload