dotConnect for PostgreSQL Documentation
Devart.Data.PostgreSql Namespace / PgSqlConnection Class / Notification Event
Example

Notification Event
Occurs when PostgreSQL server provides a notification message.
Syntax
'Declaration
 
Public Event Notification As PgSqlNotificationEventHandler
 
Event Data

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.

PropertyDescription
Gets the additional information passed from the notifying process.  
Gets the name of the notify condition.  
Gets the ID of the notifying back-end process.  
Remarks
This event is fired when you receive a notification through LISTEN/NOTIFY mechanism. In addition to this event, you can use the PgSqlAlerter class for working with PostgreSQL notifications.
Example
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
Requirements

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

See Also