读书人

哪位高手告诉上IDispose接口如何用

发布时间: 2012-08-02 11:35:26 作者: rapoo

谁告诉下IDispose接口怎么用?
我有个winform程序,现在内存一直运行会不断增加,我所有控件能dispose和close的全部都用了,但是内存还是在增加,
网上看了下用IDispose,但是不知道怎么用。。
比如我自定义一个CloseForm方法,里面就是释放所有组件,那我这个怎么转接到IDispose上呢?

C# code
        /// <summary>        /// 关闭窗体        /// </summary>        private void CloseFrom()        {            if (t != null && t.IsAlive)            {                t.Abort();                t = null;            }            if (thumbnail != null)            {                thumbnail = null;                thumbnail.Dispose();            }            if (tempBit != null)            {                tempBit.Dispose();                tempBit = null;            }            if (tempBitmap != null)            {                tempBitmap.Dispose();                tempBitmap = null;            }            if (dsImage != null)            {                dsImage.Dispose();                dsImage = null;            }            if (img != null)            {                img.Dispose();                img = null;            }            #region 释放控件            this.pbHeadPicture.Image.Dispose();            this.pbHeadPicture.Dispose();            this.pbHeadPicture = null;            this.pnlAdjustment.BackgroundImage.Dispose();            this.pnlAdjustment.Dispose();            this.pnlAdjustment = null;            this.pnlGender2.Dispose();            this.pnlGender2 = null;            this.pnlPhotoBG.Dispose();            this.pnlPhotoBG = null;            this.picClose.Image.Dispose();            this.picClose.Dispose();            this.picClose = null;            this.picFemale.Dispose();            this.picFemale.Dispose();            this.picFemale = null;            this.picGenderOK.Image.Dispose();            this.picGenderOK.Dispose();            this.picGenderOK = null;            this.picGenderReturn.Image.Dispose();            this.picGenderReturn.Dispose();            this.picGenderReturn = null;            this.picJia.Image.Dispose();            this.picJia.Dispose();            this.picJia = null;            this.picJian.Image.Dispose();            this.picJian.Dispose();            this.picJian = null;            this.picLeft.Image.Dispose();            this.picLeft.Dispose();            this.picLeft = null;            this.picMale.Image.Dispose();            this.picMale.Dispose();            this.picMale = null;            this.picOK.Image.Dispose();            this.picOK.Dispose();            this.picOK = null;            this.picReturn.Image.Dispose();            this.picReturn.Dispose();            this.picReturn = null;            this.picRight.Image.Dispose();            this.picRight.Dispose();            this.picRight = null;            this.picJiaoCheng.Image.Dispose();            this.picJiaoCheng.Dispose();            this.picJiaoCheng = null;            #endregion            if (apfShowPicture != null)            {                apfShowPicture.Dispose();                apfShowPicture = null;            }            if (transKuangForm != null)            {                transKuangForm.Dispose();                transKuangForm.Close();                transKuangForm = null;            }            if (controlForm != null)            {                controlForm.Dispose();                controlForm.Close();                controlForm = null;            }            if (dengDaiForm != null)            {                dengDaiForm.Dispose();                dengDaiForm.Close();                dengDaiForm = null;            }            this.Dispose();        }//怎么和这个结合起来呢?class R:IDisposable  {      R()      {}      ~R()      {          Dispose(false);      }      public void Dispose()      {          GC.SuppressFinalize(this);          Dispose(true);      }      protected virtual void Dispose(bool fDisposing)      {          if(disposed_)          {              return;          }          if(fDisposing)          {              Console.WriteLine(“Free managed resources”);          }          Console.WriteLine(“Free unmanaged resources”);          dispose_ = true;      }      private bool disposed_ = false;  }   //小弟新手啊,望高手赐教~! 



[解决办法]
http://www.cnblogs.com/suningnet/archive/2010/06/12/1757447.html
[解决办法]
让一个类 继承IDisposable接口 然后在别的地方调用的时候就可以使用using() 那个类在这里面就可以自动释放资源拉。
[解决办法]
内存增加未必是释放就可以解决的,GC一般会根据内存情况适时而定的。

你检查一下代码,看看是那一个功能或窗口导致内存快速上升的

读书人网 >C#

热点推荐