title: How can I access a member from another class with a Timer?
body: <p>I'm currently writing a class to calculate the average download speed over a defined period of time, taking a defined number of samples. The way I thought this would work is that this class runs a Timer object, which calls a method inside said class that will look at the bytes downloaded (maintained in a parent class, the FTPDownloadFile), and then store that sample in a Queue. My issue is accessing the number of bytes downloaded, however.</p>
  
  <p>My method of accessing that information was through a reference that was passed in when the download calculating class was constructed, however, it seems like I'm not understanding/using references correctly. The variable that is passed in always appears to be 0, even though I can see the original variable changing.</p>
  
  <p>Can anyone tell me what I'm doing wrong / suggest a better way for me to accomplish what I want to do?</p>
  
  <p>First, here is the class that is handling the calculation of the download speed:</p>
  
  <pre><code>public class SpeedCalculator
      {
          private const int samples = 5;
          private const int sampleRate = 1000; //In milliseconds
          private int bytesDownloadedSinceLastQuery;
          private System.Threading.Timer queryTimer;
          private Queue&lt;int&gt; byteDeltas = new Queue&lt;int&gt;(samples);
          private int _bytesDownloaded;
  
          public SpeedCalculator(ref int bytesDownloaded)
          {
              _bytesDownloaded = bytesDownloaded;
          }
  
          public void StartPolling()
          {
              queryTimer = new System.Threading.Timer(this.QueryByteDelta, null, 0, sampleRate);
          }
  
          private void QueryByteDelta(object data)
          {
              if (byteDeltas.Count == samples)
              {
                  byteDeltas.Dequeue();
              }
  
              byteDeltas.Enqueue(_bytesDownloaded - bytesDownloadedSinceLastQuery);
              bytesDownloadedSinceLastQuery = _bytesDownloaded;
          }
  
          /// &lt;summary&gt;
          /// Calculates the average download speed over a predefined sample size.
          /// &lt;/summary&gt;
          /// &lt;returns&gt;The average speed in bytes per second.&lt;/returns&gt;
          public float GetDownloadSpeed()
          {
              float speed;
              try
              {
                  speed = (float)byteDeltas.Average() / ((float)sampleRate / 1000f);
              }
              catch {speed = 0f;}
  
              return speed;
          }
  </code></pre>
  
  <p>That class is contained inside of my FTPDownloadFile class:</p>
  
  <pre><code>class FTPDownloadFile : IDisposable
  {
      private const int recvBufferSize = 2048;
      public int bytesDownloaded;
      public SpeedCalculator Speed;
      private FileStream localFileStream;
      FtpWebResponse ftpResponse;
      Stream ftpStream;
      FtpWebRequest ftpRequest;
      public List&lt;string&gt; log = new List&lt;string&gt;();
      private FileInfo destFile;
  
      public event EventHandler ConnectionEstablished;
  
      public FTPDownloadFile()
      {
          bytesDownloaded = 0;
          Speed = new SpeedCalculator(ref bytesDownloaded);
      }
  
      public void GetFile(string host, string remoteFile, string user, string pass, string localFile)
      {
          //Some code to start the download...
          Speed.StartPolling();
      }
  
      public class SpeedCalculator {...}
  }
  </code></pre>