我有一个Windows应用程序,它使用一个Notifier来捕获带有Entity的SQLDependency变更事件,并且一切都很好。EntityChangeNotifier是一个详细阐述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)
{
}
}
}我希望在这段代码中继续侦听要比While(true)更好。我该怎么做呢?
如果需要,可以在这里找到完整的项目结构:enter link description here
感谢大家
发布于 2016-04-15 09:39:54
退出notifer方法时,使用语句正在处理StartNotifier。删除使用和在位状态,将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);
}
}https://stackoverflow.com/questions/36642648
复制相似问题