title: Trouble with memory in C#
body: <p>I want to draw a loading circle (.gif) on the screen, while the applications is loading something big. But I can't run the circle as fast as I want, because I get memory issues. Does anyone know how to solve these (on 75 ms instead of 1000ms)? And how to remove the circle when done (it doesn't disappear any more).</p>
  
  <p>EDIT: The load function is executed when the window size changes.</p>
  
  <pre><code>    public Main()
      {
          InitializeComponent();
          StartUp();
          WindowState = FormWindowState.Maximized;
      }
  
      bool onrun;
      bool done;
      System.Threading.Timer timer;
      GifImage Circle;
      Point center;
  
      void StartUp()
      {
          onrun = true;
          done = false;
          timer = new System.Threading.Timer(new System.Threading.TimerCallback(Animate));
          timer.Change(0, 500);
      }
  
      void Animate(object sender)
      {
          if (onrun == true)
          {
              Circle = new GifImage("circleAnim.gif");
              Circle.ReverseAtEnd = false;
              int width = Screen.PrimaryScreen.Bounds.Width;
              int height = Screen.PrimaryScreen.Bounds.Height;
              center = new Point((width / 2) - 150, (height / 2) - 150);
              onrun = false;
          }
          else if (done == true)
          {
              timer.Dispose();
          }
  
          Image i = Circle.GetNextFrame();
          System.Drawing.Graphics GraphicsObject = Graphics.FromHwnd(IntPtr.Zero);
          try
          {
              GraphicsObject.DrawImage(i, center);
              i.Dispose();
              GraphicsObject.Dispose();
          }
          catch { }
      }
  
      private void Load(object sender, EventArgs e)
      {
          int hduizend = 100000;
  
          for (int i = 1; i &lt;= 100000000; i++)
          {
              hduizend /= 2;
              hduizend *= 2;
          }
  
          done = true;
      }
  </code></pre>
  
  <p>EDIT2 (error):
  "Attempted to read or write protected memory. This is often an indication that other memory is corrupt".</p>
  
  <p>Also the gif image itself is not the problem:</p>
  
  <pre><code>public class GifImage
  {
  private Image gifImage;
  private FrameDimension dimension;
  private int frameCount;
  private int currentFrame = -1;
  private bool reverse;
  private int step = 1;
  
  public GifImage(string path)
  {
      gifImage = Image.FromFile(path); 
      dimension = new FrameDimension(gifImage.FrameDimensionsList[0]); 
      frameCount = gifImage.GetFrameCount(dimension); 
  }
  
  public bool ReverseAtEnd 
  {
      get { return reverse; }
      set { reverse = value; }
  }
  
  public Image GetNextFrame()
  {
  
      currentFrame += step;
  
  
      if (currentFrame &gt;= frameCount || currentFrame &lt; 1)
      {
          if (reverse)
          {
              step *= -1;
              currentFrame += step; 
          }
          else
              currentFrame = 0;
      }
      return GetFrame(currentFrame);
  }
  
  public Image GetFrame(int index)
  {
      gifImage.SelectActiveFrame(dimension, index); 
      return (Image)gifImage.Clone(); 
  }
  </code></pre>
  
  <p>}</p>