在C#中获得处置倒闭?

2023-09-03 00:43:34 作者:-不做畜男好多年

我调查微软企业库(数据应用程序块) - 样品SLN 的

他们有异步读取数据( IAsync ,虽然新版本(6)也支持异步样本)。

They have a sample of reading data asynchronously ( IAsync , although the new ver (6) also support async).

但ReSharper的(或视觉工作室 - 没关系的)显示我:访问到位于封闭:(首先,我将显示图像,因此它会更清楚,然后我会贴上code)

But Resharper(or visual studio- nevermind) shows me : "Access to disposed closure" : (first i will show the image , so it will be clearer , then i'll paste the code )

code:

/*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核对信号(它运行在一个循环中),它的 prevents 的使用子句达成 - 这意味着 - 没有处理将调用

Why is it giving this warning ? there is a manualreset-checked-signal (which runs in a loop) which prevents the using clause to be reached - which means - no dispose will call .

那么,为什么它大喊(报警)?

So why does it yell (warning) ?

推荐答案

您传递 doneWaitingEvent 来一个lambda可能超出使用块的范围。即还有的认为处置危险拉姆达执行时会被调用。

You pass doneWaitingEvent to a lambda that may extend beyond the scope of the using block. I.e. there's a risk that Dispose will have been called when the lambda executes.

相关推荐