如何解决窗口形式的内存泄漏

时间:2015-08-24 11:17:31

标签: c# memory

我的问题是每次每秒呼叫时内存增加5MB闪烁。

我制作了窗口闪烁效果窗体

[闪烁形式]

 public partial class WarningBoxControls : Form
    {
        bool _isShadow;
        public WarningBoxControls(WarningBoxType _Type)
        {
            InitializeComponent();
            this.FormClosing += WarningBox_FormClosing;
            this.VisibleChanged +=WarningBoxControls_VisibleChanged;
            if (_Type == WarningBoxType.WarningBox_Speed)
            {
                MessageTitle.Text = "속력 경고";
                MessageContent.Text = "자동차 속도를 줄여주세요!";
                this.BackColor = Color.SkyBlue;
                pictureBox1.Image = Properties.Resources.warning_speed;

            }
            else
            {
                MessageTitle.Text = "Logger 오류";
                MessageContent.Text = "Logger가 활성화되지 않았습니다!";
                this.BackColor = Color.MediumVioletRed;
                pictureBox1.Image = Properties.Resources.warning_error;
            }

        }

        private void WarningBoxControls_VisibleChanged(object sender, EventArgs e)
        {
            this.Opacity = 0.01;
            if(this.Visible == true)
            {
                timer1.Start();
            }
            else
            {
                timer1.Stop();
            }
        }
        private void WarningBox_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (pictureBox1.Image != null)
            {
                pictureBox1.Image = null;
                pictureBox1.Dispose();
            }
            if (timer1.Enabled == true)
            {
                timer1.Stop();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (_isShadow == false)
            {
                if (this.Opacity <= 1.0)
                {
                    this.Opacity += 0.8;
                    if (this.Opacity >= 1.0)
                    {
                        _isShadow = true;
                    }
                }
            }
            else
            {
                if (this.Opacity >= 0.01)
                {
                    this.Opacity -= 0.08;
                    if (this.Opacity <= 0.01)
                    {
                        _isShadow = false;
                    }
                }
            }
        }
    }

我创建了一个类来加载闪烁效果表单。

[加载ficker表单]

public class WarningBox
    {
        WarningBoxControls _control;
        Form owner;

        public WarningBox(IWin32Window _owner, WarningBoxType _Type)
        {
            if (_owner != null)
            {
                owner = (Form)_owner;

                _control = new WarningBoxControls(_Type);
                _control.StartPosition = FormStartPosition.Manual;
                _control.Padding = new Padding(0, 0, 0, 0);
                _control.ControlBox = false;
                _control.ShowInTaskbar = false;
                _control.Size = new Size(owner.Size.Width - 40, _control.Height - 20);


            }
        }

        public void Show()
        {
            _control.Location = new Point(owner.Location.X + 20, owner.Location.Y + ((owner.Height - _control.Height) / 2));
            //_control.ShowDialog();
            //_control.BringToFront();

            if(_control.Visible != true)
            {
                _control.Show();
                _control.BringToFront();
            }


        }
        public void Hide()
        {
            if(_control.Visible != false)
            {
                //_control.Visible = false;
                _control.Hide();

            }

        }
        ~WarningBox()
        {
            if(_control != null)
            {
                _control.Dispose();
                _control = null;
            }
        }
    }

[拨打第二种方法]

private void timer1_Tick(object sender, EventArgs e)
        {
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                ReadLoggerStatus();

            }

    private void ReadLoggerStatus()
            {
                if (_Lights != null)
                {
                    if (_Lights.ERR == 0) // 에러 없을때
                    {
                        pb_error.Image = Properties.Resources.none;
                        _WarningBox.Hide();
                        btn_stop.Enabled = true;
                        btn_start.Enabled = false;
                    }
                    else
                    {

                        pb_error.Image = Properties.Resources.error;
                        _WarningBox.Show();
                        btn_stop.Enabled = false;
                        btn_start.Enabled = true;

                    }
                }
            }

并检查每秒一个错误,拨打Show()的{​​{1}}和Hide()。但是这里(WarningBox)是一个问题。

我的问题是每次每秒调用一次内存增加5MB。

我想知道它是解决最好的方法。

请让我知道答案。

事先,我会向你表示感谢。

祝你有个美好的一天!

0 个答案:

没有答案