我正在调查微软企业库(数据应用程序块) - 样本sln。
他们有一个异步读取数据的样本(IAsync
,虽然新版本(6)也支持async
)。
但Resharper(或视觉工作室 - 没关系)向我展示:“访问处理关闭”:(首先我会显示图像,所以它会更清晰,然后我会粘贴代码)
代码:
/*1*/ [Description("Execute a command that retrieves data asynchronously")]
/*2*/ static void ReadDataAsynchronously()
/*3*/ {
/*4*/ if (!SupportsAsync(asyncDB)) return;
/*5*/
/*6*/ using(var doneWaitingEvent = new ManualResetEvent(false))
/*7*/ using(var readCompleteEvent = new ManualResetEvent(false))
/*8*/ {
/*9*/ try
/*10*/ {
/*11*/ // Create command to execute stored procedure and add parameters
/*12*/ DbCommand cmd = asyncDB.GetStoredProcCommand("ListOrdersSlowly");
/*13*/ asyncDB.AddInParameter(cmd, "state", DbType.String, "Colorado");
/*14*/ asyncDB.AddInParameter(cmd, "status", DbType.String, "DRAFT");
/*15*/ // Execute the query asynchronously specifying the command and the
/*16*/ // expression to execute when the data access process completes.
/*17*/ asyncDB.BeginExecuteReader(cmd,
/*18*/ asyncResult = >
/*19*/ {
/*20*/ // Lambda expression executed when the data access completes.
/*21*/ doneWaitingEvent.Set();
/*22*/ try
/*23*/ {
/*24*/ using(IDataReader reader = asyncDB.EndExecuteReader(asyncResult))
/*25*/ {
/*26*/ Console.WriteLine();
/*27*/ Console.WriteLine();
/*28*/ DisplayRowValues(reader);
/*29*/ }
/*30*/ }
/*31*/ catch (Exception ex)
/*32*/ {
/*33*/ Console.WriteLine("Error after data access completed: {0}", ex.Message);
/*34*/ }
/*35*/ finally
/*36*/ {
/*37*/ readCompleteEvent.Set();
/*38*/ }
/*39*/ }, null);
/*40*/
/*41*/ // Display waiting messages to indicate executing asynchronouly
/*42*/ while (!doneWaitingEvent.WaitOne(1000))
/*43*/ {
/*44*/ Console.Write("Waiting... ");
/*45*/ }
/*46*/
/*47*/ // Allow async thread to write results before displaying "continue" prompt
/*48*/ readCompleteEvent.WaitOne();
/*49*/ }
/*50*/ catch (Exception ex)
/*51*/ {
/*52*/ Console.WriteLine("Error while starting data access: {0}", ex.Message);
/*53*/ }
/*54*/ }
/*55*/ }
题 :
为什么要发出这个警告?有一个manualreset-checked-signal
(在一个循环中运行),它阻止达到using
条款 - 这意味着 - 没有dispose
将调用。
为什么它会大喊(警告)?
您将doneWaitingEvent
传递给lambda,该lambda可能超出using块的范围。即当lambda执行时,有一个风险就是调用Dispose
。
它会大声警告,因为引擎不够聪明,无法确定在委托代码完成之前永远不会退出using块。这就是为什么这是一个警告而不是错误。
您可以放心地忽略此警告,您可以让resharper通过包含带有特殊注释的行来抑制警告
asyncDB.BeginExecuteReader(cmd, asyncResult =>
{
// Lambda expression executed when the data access completes.
// ReSharper disable AccessToDisposedClosure
doneWaitingEvent.Set();
// ReSharper restore AccessToDisposedClosure
try
{
using (IDataReader reader = asyncDB.EndExecuteReader(asyncResult))
{
Console.WriteLine();
Console.WriteLine();
DisplayRowValues(reader);
}
}
catch (Exception ex)
{
Console.WriteLine("Error after data access completed: {0}", ex.Message);
}
finally
{
// ReSharper disable AccessToDisposedClosure
readCompleteEvent.Set();
// ReSharper restore AccessToDisposedClosure
}
}, null);
你看到ReSharper警告的原因是ReSharper的代码流分析引擎不够强大,无法看到发生了什么:他们认为你的代码可以在没有设置using
的情况下到达doneWaitingEvent
子句的末尾,这是不可能的while
循环:
while (!doneWaitingEvent.WaitOne(1000)) {
Console.Write("Waiting... ");
}
循环将继续打印"Waiting... "
线,直到调用doneWaitingEvent.Set();
,防止您的代码到达using
块的末尾。另一个警告也是如此。
长话短说,这个警告可以安全地忽略。添加ReSharper的“忽略此警告”注释,并可选择向他们提交错误报告。