No notification inner using sentence
ayams opened this issue · 3 comments
ayams commented
You have this example:
using (var dep = new SqlTableDependency(_con, "Customers", mapper: mapper));
{
dep.OnChanged += Changed;
dep.Start();
Console.WriteLine("Press a key to exit");
Console.ReadKey();
dep.Stop();
}
but it doesn't work if I use the sentence "using". If I set out the instance out of "using" it works. Why?
christiandelbianco commented
i cannot understend. can you explain better the problem?
ayams commented
Hi Chris,
Sorry for my English but I hope to explain better with these examples:
When I use the "using" on my code sentence:
using (var dep = new SqlTableDependency(_con, "Customers", mapper: mapper))
{
dep.OnChanged += Changed;
dep.Start();
}
I can't receive the notification from SQL Server. I update any field on the Customer table and not receive the notification, but when I write my code sentence whitout "using" like this:
var dep = new SqlTableDependency(_con, "Customers", mapper: mapper);
dep.OnChanged += Changed;
dep.Start();
And I update any field over the Customer table I can receive the notification.
My doubt is, Why I can't receive the notification when write "using" in my code sentence?
I'm using SQL Server 2012 and C# 7.2
thanks
…________________________________
From: Christian Del Bianco ***@***.***>
Sent: Friday, March 12, 2021 7:42:36 AM
To: christiandelbianco/monitor-table-change-with-sqltabledependency ***@***.***>
Cc: ayams ***@***.***>; Author ***@***.***>
Subject: Re: [christiandelbianco/monitor-table-change-with-sqltabledependency] No notification inner using sentence (#225)
i cannot understend. can you explain better the problem?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub<#225 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AEG4AXTA2QEBI4KPWWAKDB3TDIK4ZANCNFSM4ZBFA23A>.
christiandelbianco commented
you are using in the WRONG way.
to receive notifications, SqlTableDependency instance must be "live".
When the code goes out of the using statement, SqlTableDependency instance is DISPOSED.
In my example infact, there is a Console.ReadKey() to stop the current thread to goes out of the using statement.