I'm trying to implement sqldependency in my vb.net application as per:
http://www.dreamincode.net/forums/topic/156991-using-sqldependency-to-monitor-sql-database-changes/
I'm having problems with the involking code, when a notification is recieved the application locks up with no errors. (When MyOnChanged is called via the deligate)
Have I made a mistake in my C# > VB.net conversion?
Private Sub GetNames()
Dim connectionString As String = "String..."
ListView1.Items.Clear()
SqlDependency.[Stop](connectionString)
SqlDependency.Start(connectionString)
Using cn As New SqlConnection(connectionString)
Using cmd As SqlCommand = cn.CreateCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT [finish_time], [file_name] FROM dbo.[log_table];"
cmd.Notification = Nothing
Dim dep As New SqlDependency(cmd)
AddHandler dep.OnChange, New OnChangeEventHandler(AddressOf MyOnChanged)
cn.Open()
Using dr As SqlDataReader = cmd.ExecuteReader()
While dr.Read()
Dim ls As New ListViewItem(dr.Item("finish_time").ToString.Trim())
ls.SubItems.Add(dr.Item("file_name").ToString.Trim())
ListView1.Items.Add(ls)
End While
End Using
End Using
End Using
End Sub
Private Sub MyOnChanged(ByVal sender As Object, ByVal e As SqlNotificationEventArgs)
If ListView1.InvokeRequired Then
ListView1.Invoke(New MethodInvoker(AddressOf GetNames))
Else
GetNames()
End If
Dim dep As SqlDependency = TryCast(sender, SqlDependency)
AddHandler dep.OnChange, New OnChangeEventHandler(AddressOf MyOnChanged)
End Sub