首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >继续收听SQLDependency

继续收听SQLDependency
EN

Stack Overflow用户
提问于 2016-04-15 08:58:19
回答 1查看 170关注 0票数 0

我有一个Windows应用程序,它使用一个Notifier来捕获带有Entity的SQLDependency变更事件,并且一切都很好。EntityChangeNotifier是一个详细阐述SQLDependency的项目。

调用while (true)时,我可以继续侦听,当我进行更改时,代码输入到notifer.Changed += (sender, e)

代码语言:javascript
复制
        private StartNotifier()
        {
            var info = new SABIntegrationEntities();
            // Notifier
            using (var notifer = new EntityChangeNotifier<SpRicezioneSpedizioniLightNotifier, GemapDbContext>(p => p.SPEDIZIONE_STATO_GENERAZIONE == "I" || p.SPEDIZIONE_STATO_GENERAZIONE == "U"))
            {
                notifer.Error += (sender, e) =>
                {
                    Log.Error(String.Format("[{0}, {1}, {2}]:\n{3}", e.Reason.Info, e.Reason.Source, e.Reason.Type, e.Sql));
                };
                notifer.Changed += (sender, e) =>
                {
                    e.ContinueListening = false;
                    bool result = true;
                    var spedizioniI = info.SpRicezioneSpedizioniLights.Where(x => x.SPEDIZIONE_STATO_GENERAZIONE == "I" || x.SPEDIZIONE_STATO_GENERAZIONE == "U");
                    foreach (var p in spedizioniI)
                        {
                            p.SPEDIZIONE_STATO_GENERAZIONE = "G";
                        }
                    }
                    e.ContinueListening = true;
                };
                while (true)
                {
                }
           }
       }

我希望在这段代码中继续侦听要比While(true)更好。我该怎么做呢?

如果需要,可以在这里找到完整的项目结构:enter link description here

感谢大家

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-15 09:39:54

退出notifer方法时,使用语句正在处理StartNotifier。删除使用和在位状态,将notifer作为私有类字段,实现IDisposable接口,并在Dispose方法中配置notifer。在未释放包含StartNotifier方法的类之前,应接收消息。

编辑:为您提供一个想法的代码片段:

代码语言:javascript
复制
public partial class Form1 : Form
{
    private readonly EntityChangeNotifier<SpRicezioneSpedizioniLightNotifier, GemapDbContext> _entityChangeNotifier;
    public Form1()
    {
        InitializeComponent();
        _entityChangeNotifier = StartNotifier();
    }

    private EntityChangeNotifier<SpRicezioneSpedizioniLightNotifier, GemapDbContext> StartNotifier()
    {
        var notifer = new EntityChangeNotifier<SpRicezioneSpedizioniLightNotifier, GemapDbContext>(
            p => p.SPEDIZIONE_STATO_GENERAZIONE == "I" || p.SPEDIZIONE_STATO_GENERAZIONE == "U");
        notifer.Error += (sender, e) => { /*log*/ };
        notifer.Changed += (sender, e) => { /*action when chagned*/};
        return notifer;
    }
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
            _entityChangeNotifier.Dispose();
        }
        base.Dispose(disposing);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36642648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档