Page 1 of 1

Notify, Listen Docs or example

Posted: Tue 18 Nov 2008 14:20
by twynne
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

Posted: Fri 21 Nov 2008 10:50
by Shalex
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.