title: How can I de-reference an object in C#?
body: <p>I have a buffer that cycles between two indices and I want to write out the object at the current index in a task and allow the rest of the program to continue processing things. I have attempted to simplify the process while maintaining all the pertinent parts.</p>
  
  <pre><code>    object[] buffer = new object[2]
      int currentIndex = 0
  
      while(true){
          buffer[currentIndex].field1 = newdatahere //data grabbed by sensor bundle
          buffer[currentIndex].field2 = newdatahere //data grabbed by camera bundle
          buffer[currentIndex].field3 = newdatahere //data grabbed from system snapshot
  
          task.factory.starnew(()=&gt;{
              writeOutObject(buffer[currentIndex])
          }
  
          buffer[currentIndex] = new object();
  
          currentIndex = 1 - currentIndex //cycle between the 0 and 1 indices
      }
  
      void writeOutObject(Object obj){
          //do file IO here
          //write out field1, field2, field3
      }
  </code></pre>
  
  <p>The problem is that by assigning the buffer item to a new object I am killing the writeOutObject method because the obj no longer exists by the time the task runs. I want to be able to keep the old object until it is written out and have the buffer point to a new object.</p>
  
  <p>What I want to do:</p>
  
  <pre><code>    object obj1 = new object();
      obj1.field1 = data1;
      obj1.field2 = data2;
      obj1.field3 = data3;
  
      obj2 = obj1;
      //de-reference obj1 from the object that it was pointed to and associate it to a new object
  
      // i want this to write out data1,data2,data3 but instead it is 
      // writing out data4,data5,data6 or some mixture because it has 
      // been overwritten halfway through the file IO
      task.factory.startnew(()=&gt;{ write out obj2 } 
  
      obj1.field1 = data4;
      obj1.field2 = data5;
      obj1.field3 = data6;
  </code></pre>
  
  <p>Maybe something like:</p>
  
  <pre><code>    obj1 = new object()
  
      obj2* = &amp;obj1
      obj1* = &amp;new object
  </code></pre>
  
  <p>I need to break the reference of obj1 back to obj2 once it has been assigned. Simply doing this won't work:</p>
  
  <pre><code>    obj1 = new object()
      obj2 = obj1
      obj1 = null // or new object()
  </code></pre>
  
  <p>As requested, "The Real Code"</p>
  
  <pre><code>    CancellationTokenSource cts = new CancellationTokenSource();
      public void StartMachine()
      {
          Task.Factory.StartNew(() =&gt;
              {
                  _isFirstData = true;
                  _expiredFlag = false;
                  Plc.StartPLC();
                  Plc.Start();
  
                  while (true)
                  {
                      if (!_paused &amp;&amp; !Plc.IsInputStackEmpty() &amp;&amp; !Plc.IsOutputSlideOpen())
                      {
  
                          CameraFront.SnapAquire();
  
                          // If this is the first data set the wait handles
                          if (!_isFirstData)
                          {
                              CameraBack.SnapAquire();
                          }
                          else
                          {
                              _imageBackRecieved.Set();
                              _databaseInfoRecieved.Set();
                              //_isFirstCard = false;
                          }
  
                          // Wait for 3 things! Image Front, Image Back, Database
                          bool gotEvents = WaitHandle.WaitAll(_waitHandles, TIMEOUT);
                          if (gotEvents)
                          {
                              if (!_isFirstData)
                              {
                                  if (Buffer[1 - NextDataOutIndex].IsDataComplete())
                                  {
                                      if (Buffer[1 - NextDataOutIndex].EvaluateData()) 
                                      {
                                          OnPassFailNotification()
                                          Plc.Pass();
                                      }
                                      else
                                      {
                                          OnPassFailNotification()
                                          Plc.Fail();
                                      }
                                  }
                                  else
                                  {
                                      OnPassFailNotification()
                                      Plc.Fail();
                                      Common.Logging
                                  }
                              }
                              else
                              {
                                  _isFirstData = false;
                              }
                          }
                          else
                          {
                             Common.Logging("WARNING: Wait handle timed out"
                              Plc.Fail();
                          }
  
                          Data temp = Buffer[1 - NextDataOutIndex];
                          Task.Factory.Startnew(()=&gt;{
                              Data.WriteData(temp);
                          }
                          Buffer[1 - NextDataOutIndex] = new Data(); 
  
                          // Swap card buffers - alternate between 1 and 0
                          NextdataOutIndex = 1 - NextDataOutIndex;
  
                          // Do this
                          Plc.WheelAdvance();
                      }
                      else
                      {
                      }
                  }
              }, cts.Token);                
      }
  
  
      public static void WriteData(Data data)
      {
          if(WRITE_BATCH_FILES)
          try
          {
              if (data.ImageFront != null)
              {
                  string filenameforfront = "blahlbah-front.tiff";
                  OperatorSet.WriteImage(data.ImageFront, "tiff", 0, filenameforfront);
              }
              if (data.ImageBack != null)
              {
                  string filenameforback = "blahblah-back.tiff";
                  HOperatorSet.WriteImage(data.ImageBack, "tiff", 0, filenameforback);
              }
          }
          catch (Exception ex)
          {
              Common.Logging.
              //throw ex;
          }
  
          //TODO: Write out data in xml
          //TODO: Write out metrics
      }
  </code></pre>