The event handler receives an argument of type PgSqlNotificationEventArgs containing data related to this event. The following PgSqlNotificationEventArgs properties provide information specific to this event.
Property | Description |
---|
AdditionalMessage | Gets the additional information passed from the notifying process. |
Condition | Gets the name of the notify condition. |
ProcessId | Gets the ID of the notifying back-end process. |
The following example demonstrates using Listening with dotConnect for PostgreSQL. You can execute "notify "listen_point"" at the server side to check this code.
using System;
using Devart.Data.PostgreSql;
using System.Threading;
namespace ConsoleApplication3 {
class Program {
static System.Threading.Timer timer;
static PgSqlConnection connection;
static void Main(string[] args) {
connection = new PgSqlConnection();
connection.ConnectionString = "host=db; user id=postgres; password=postgres; database=postgres; schema=public; port=5438;";
connection.Open();
connection.Notification += new PgSqlNotificationEventHandler(ConnectionNotification);
PgSqlCommand listenCommand = new PgSqlCommand("listen \"listen_point\"", connection);
listenCommand.ExecuteNonQuery();
System.Threading.TimerCallback timerDelegate = new TimerCallback(Check);
timer = new System.Threading.Timer(timerDelegate, null, 1000, 3000);
Console.ReadLine();
}
private static void ConnectionNotification(object sender, PgSqlNotificationEventArgs e) {
string s = e.Condition;
if (s != null && s == "listen_point") {
// TODO: this code will be executed
}
}
private static void Check(Object stateInfo) {
//some command should be executed to check the notification
PgSqlCommand listenCommand = new PgSqlCommand("select 1", connection);
listenCommand.ExecuteNonQuery();
}
}
}
Imports System
Imports Devart.Data.PostgreSql
Imports System.Threading
Module Module1
Dim timer As System.Threading.Timer
Dim connection As PgSqlConnection
Sub Main()
connection = New PgSqlConnection()
connection.ConnectionString = "host=db; user id=postgres; password=postgres; database=postgres; schema=public; port=5438;"
connection.Open()
AddHandler connection.Notification, New PgSqlNotificationEventHandler(AddressOf ConnectionNotification)
Dim listenCommand As New PgSqlCommand("listen ""listen_point""", connection)
listenCommand.ExecuteNonQuery()
Dim timerDelegate As New TimerCallback(AddressOf Check)
timer = New System.Threading.Timer(timerDelegate, Nothing, 1000, 3000)
Console.ReadLine()
End Sub
Private Sub ConnectionNotification(ByVal sender As Object, ByVal e As PgSqlNotificationEventArgs)
Dim s As String = e.Condition
If (s <> Nothing And s = "listen_point") Then
' TODO: this code will be executed
End If
End Sub
Private Sub Check(ByVal stateInfo As Object)
'some command should be executed to check the notification
Dim listenCommand As New PgSqlCommand("select 1", connection)
listenCommand.ExecuteNonQuery()
End Sub
End Module
Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2