title: Access to disposed closure in C#?
body: <p><em>I'm investigating Microsoft enterprise library  (data application block)  -- The  samples sln.</em></p>
  
  <p>They have a sample of reading data asynchronously ( <code>IAsync</code> , although the new ver (6) also support <code>async</code>).</p>
  
  <p>But Resharper(<em>or visual studio- nevermind</em>) shows me  : "Access to disposed closure" : (first i will show the image , so it will be clearer  , then i'll paste the code ) </p>
  
  <p><img src="https://i.stack.imgur.com/v6vU9.png" alt="enter image description here"></p>
  
  <p>code : </p>
  
  <pre><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 = &gt;
  /*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*/    }
  </code></pre>
  
  <p>Question : </p>
  
  <p>Why is it giving this warning ? there is a <code>manualreset-checked-signal</code> (which runs in a loop) which <strong>prevents</strong> the <code>using</code> clause to be reached - which means  - no <code>dispose</code> will call .</p>
  
  <p>So why does it yell (warning) ?</p>