Npgsql-手册

2026/4/26 14:57:50

Console.WriteLine(\ t.Start(); Console.WriteLine(command.ExecuteScalar()); conn.Close(); } catch(NpgsqlException e) { if (e.Code == \ Console.WriteLine(\cancelled\ } } public static void CancelRequest() { command.Cancel(); Console.WriteLine(\ } } Working with Notifications

Npgsql allows user to receive events based on notifications sent by a server. There are two ways to receive notications with Npgsql: asynchronously or synchronously. Synchronous notification is only supported by Npgsql 1.0 and above.

苏州纳米科技发展有限公司 Nanopolis Suzhou Co., Ltd 苏州工业园区金鸡湖大道99号苏州纳米城中北区23幢综合楼

Bldg CN-23, Nanopolis Suzhou, 99 Jinji Lake Avenue, Suzhou Industrial Park, Jiangsu Province, P.R. China 215123

电话:+86 0512 69993000 传真: +86 0512 69993999 网站:www.nanopolis.cn

49

Asynchronous notifications

This is the default notification mechanism used in Npgsql. It is called asynchronous because Npgsql doesn't receive a notification upon execution of the event which generated it on the server. Npgsql receives the notification on the next instance of client interaction with the server. This interaction actually occurs when Npgsql sends a subsequent command to the server- which might consist of a few seconds to many hours later. With this in mind, most users will need to actively poll the server in order to recieve notifications in a timely matter. One approach involves polling via empty commands such as \

Synchronous notifications

Starting with Npgsql 1.0, there is support for synchronous notifications. When working in this mode, Npgsql is able to receive a notificaton upon its instantiation and deliver it to client. All this is done without any additional interaction between the client and server (as described above).

Important notice: When using Synchronous notification, you can't execute commands inside your notification handler function. If you do so, you will hang Npgsql as the thread which handles the notification is the same which handles Npgsql communication with backend. If you want to use any commands, please, create another connection and use it instead. This is not the best solution but we are studying better ways to do that instead of needing another connection.

The code to receive the notification is the same for both modes:

using System; using System.Data; using Npgsql; using NpgsqlTypes; using System.Threading; 苏州纳米科技发展有限公司 Nanopolis Suzhou Co., Ltd 苏州工业园区金鸡湖大道99号苏州纳米城中北区23幢综合楼

Bldg CN-23, Nanopolis Suzhou, 99 Jinji Lake Avenue, Suzhou Industrial Park, Jiangsu Province, P.R. China 215123

电话:+86 0512 69993000 传真: +86 0512 69993999 网站:www.nanopolis.cn

50


Npgsql-手册.doc 将本文的Word文档下载到电脑
搜索更多关于: Npgsql-手册 的文档
相关推荐
相关阅读
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 10

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xuecool-com QQ:370150219