Ale
Ale

Reputation: 71

Continue listening for SQLDependency

I have a Windows Form application that use a Notifier for capture SQLDependency change event with Entity Framework and all works fine. The EntityChangeNotifier is a project that elaborate the SQLDependency.

Whit the call while (true) i can have a continue listening and when i have a change the code enter in notifer.Changed += (sender, e)

        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)
                {
                }
           }
       }

I want to have a continue listening in this code better than While(true). How can i do it?

If you want you can find the complete project structure here: enter link description here

Thank to all

Upvotes: 0

Views: 202

Answers (1)

Miłosz Wierzbicki
Miłosz Wierzbicki

Reputation: 2112

Your using statement is disposing notifer when you exit StartNotifier method. Remove using and while statments, put notifer as private class field, implement IDisposable interface and dispose notifer in Dispose method. You should receive messages until your class containing StartNotifier method is not disposed.

EDIT: Code snippet to give you an idea:

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);
    }
}

Upvotes: 1

Related Questions