Notify, Listen Docs or example

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for PostgreSQL
Post Reply
twynne
Posts: 1
Joined: Tue 18 Nov 2008 14:17

Notify, Listen Docs or example

Post by twynne » Tue 18 Nov 2008 14:20

I'm having a hard time finding information on how to implement the Notify/Listen with .connect for PostgreSQL. Is there a sample that shows this or if there is something in the documentation could you please post a link? Thanks

Shalex
Devart Team
Posts: 8080
Joined: Thu 14 Aug 2008 12:44

Post by Shalex » Fri 21 Nov 2008 10:50

Here is a sample that demonstrates using Listening with dotConnect for PostgreSQL.

Code: Select all

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Devart.Data;
using Devart.Data.PostgreSql;
using System.Threading;

namespace NotifyListenPG {
  
  public partial class Form1 : Form {
    
    public Form1() {
      InitializeComponent();
    }

    System.Threading.Timer timer;
    PgSqlConnection connection;

    private void Form1_Load(object sender, EventArgs e) {
    
      connection = new PgSqlConnection();
      connection.ConnectionString = "host=localhost; user id=postgres; password=postgres; database=test; schema=public;";

      connection.Open();
      connection.Notification += new PgSqlNotificationEventHandler(ConnectionNotification);
      PgSqlCommand listenCommand = new PgSqlCommand("listen \"listen_point\"", connection);
      listenCommand.ExecuteNonQuery();

       TimerCallback timerDelegate = new TimerCallback(Check);
       timer = new System.Threading.Timer(timerDelegate, null, 1000, 3000);
    }
                 
    private void ConnectionNotification(object sender, PgSqlNotificationEventArgs e) {
    
       string s = e.Condition;
       if (s != null && s == "listen_point") {
         
         // TODO: this code will be executed
         
        }
    }
       
    private void Check(Object stateInfo) {
      
      //some command should be executed to check the notification
      PgSqlCommand listenCommand = new PgSqlCommand("select 1", connection); 
      listenCommand.ExecuteNonQuery();
    }
  }
}
Execute "notify "listen_point"" at the server side to check this code.

Post Reply