2016-04-15 67 views
0

我有一個Windows窗體應用程序,它使用通告程序捕獲實體框架中的SQLDependency更改事件,並且所有工作都正常。 EntityChangeNotifier是一個詳細說明SQLDependency的項目。繼續監聽SQLDependency

白衣呼叫while (true)我可以有一個繼續聽,當我有一個變化的代碼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) 
       { 
       } 
      } 
     } 

進入我希望有一個繼續聽這段代碼比雖然(真)好。我該怎麼做?

如果你想你可以找到完整的項目結構在這裏:enter link description here

感謝所有

回答

1

你使用語句處置notifer當您退出StartNotifier方法。刪除使用和while語句時,將notifer作爲私有類字段,實現IDisposable接口並在Dispose中配置notifer方法。您應該收到消息,直到您的課程不包含StartNotifier方法。

編輯:代碼片段給你一個想法:

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

你能幫助我的代碼嗎? – Ale

+1

@Ale:請參閱我的編輯。 –

+0

感謝您的回覆。如果我有錯誤,我該如何處理唯一的通知程序並重新激活新的通知程序?如果我在聽程序中有相同的錯誤,我想自動重新啓動通知程序。 – Ale