Sub PgSqlAlerter_Test(ByVal connection As PgSqlConnection)
Dim alerts As String = "event1,event2,trigger event"
' Create an alerter subscribed to the alerts specified above.
Dim recipient As PgSqlAlerter = New PgSqlAlerter(connection, alerts)
' Add event handlers for the events when an alert is incoming and when the alerter stops to listen.
AddHandler recipient.Alert, New PgSqlAlerterAlertEventHandler(AddressOf recipient_Alert)
AddHandler recipient.Stopped, New PgSqlAlerterStoppedEventHandler(AddressOf recipient_Stopped)
' Register alerts and start the waiting process
recipient.Start()
Console.WriteLine("The waiting process has been started")
' Signal all registered alerts
Notify(connection)
' Wait for the notifications from server
Thread.Sleep(100)
recipient.Stop()
End Sub
Sub Notify(ByVal connection As PgSqlConnection)
' Create another instance of PgSqlAlerter that will send notifications to the server.
Dim sender As New PgSqlAlerter
sender.Connection = connection
' Signal to the server that an alert should be sent.
sender.Signal("event1")
' Send several alerts at once:
sender.Signal("event2,event1")
'The following command inserts a row into the Dept table, firing the 'dept_insert_trigger' trigger:
Dim cmd As PgSqlCommand = New PgSqlCommand("insert into dept values (50, 'IT', 'San Diego')", connection)
cmd.ExecuteNonQuery()
' The following notification name was not registered, so the client will not receive this signal
sender.Signal("unknown event")
End Sub
' A simple event handler that receives the incoming alert and prints its content.
Sub recipient_Alert(ByVal sender As Object, ByVal e As PgSqlAlerterAlertEventArgs)
Console.WriteLine("Received alert - {0}", e.AlertName)
End Sub
' A simple event handler processing the Stopped event of the alerter.
Sub recipient_Stopped(ByVal sender As Object, ByVal e As PgSqlAlerterStoppedEventArgs)
Console.WriteLine("The waiting process has been stopped")
End Sub