如何避免异常?关闭项目时无法访问已处置的对象

时间:2016-01-01 01:16:52

标签: c# .net winforms

关闭我的项目,点击右上角的form1红色x我得到了例外。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenPop;
using OpenPop.Pop3;
using OpenPop.Mime;

namespace Pop3_Emails
{
    public partial class Form1 : Form
    {
        static OpenPop.Pop3.Pop3Client cc = new Pop3Client();
        ProgressBarWithText pbt = new ProgressBarWithText();

        public Form1()
        {
            InitializeComponent();

            pbt.Size = new Size(216, 10);
            pbt.Location = new Point(8, 312);
            groupBox1.Controls.Add(pbt);

            backgroundWorker1.RunWorkerAsync();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            OpenPop.Pop3.Pop3Client PopClient = new OpenPop.Pop3.Pop3Client();
            PopClient.Connect("net.net", 110, false);
            PopClient.Authenticate("meuser", "mepass",
                OpenPop.Pop3.AuthenticationMethod.UsernameAndPassword);
            int messageCount = PopClient.GetMessageCount();
            List<OpenPop.Mime.Message> allMessages = new List<OpenPop.Mime.Message>(messageCount);
            for (int i = messageCount; i > 0; i--)
            {
                allMessages.Add(PopClient.GetMessage(i));
                int nProgress = (messageCount - i) * 100 / messageCount;
                backgroundWorker1.ReportProgress(nProgress, PopClient.GetMessageCount().ToString() + " Number of downloaded messages" + i);
            }
            PopClient.Disconnect();        
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            pbt.Value = e.ProgressPercentage;
            pbt.Text = e.ProgressPercentage.ToString() + "%";
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        }

        public class ProgressBarWithText : ProgressBar
        {
            const int WmPaint = 15;
            SizeF TextSize;
            PointF TextPos;

            public ProgressBarWithText()
            {
                this.DoubleBuffered = true;
                this.TextChanged += ProgressBarWithText_TextChanged;
                this.SizeChanged += ProgressBarWithText_SizeChanged;
            }

            public override string Text
            {
                get { return base.Text; }
                set { base.Text = value; }
            }

            void RecalcTextPos()
            {
                if (string.IsNullOrEmpty(base.Text))
                    return;

                using (var graphics = Graphics.FromHwnd(this.Handle))
                {
                    TextSize = graphics.MeasureString(base.Text, this.Font);
                    TextPos.X = (this.Width / 2) - (TextSize.Width / 2);
                    TextPos.Y = (this.Height / 2) - (TextSize.Height / 2);
                }
            }

            void ProgressBarWithText_SizeChanged(object sender, EventArgs e)
            {
                RecalcTextPos();
            }

            void ProgressBarWithText_TextChanged(object sender, EventArgs e)
            {
                RecalcTextPos();
            }

            protected override void WndProc(ref System.Windows.Forms.Message m)
            {
                base.WndProc(ref m);

                switch (m.Msg)
                {
                    case WmPaint:
                        using (var graphics = Graphics.FromHwnd(Handle))
                            graphics.DrawString(base.Text, base.Font, Brushes.Black, TextPos.X, TextPos.Y);

                        break;
                }
            }

            protected override CreateParams CreateParams
            {
                get
                {
                    CreateParams result = base.CreateParams;
                    result.ExStyle |= 0x02000000; // WS_EX_COMPOSITED 
                    return result;
                }
            }
        }
    }
}

异常位于行上的ProgressBarWithText类中:

var graphics = Graphics.FromHwnd(this.Handle)

无法访问已处置的对象

所以我现在添加了form1结束事件。 我该怎么办?什么停止和处置?

2 个答案:

答案 0 :(得分:2)

编码比解释更容易。此外,还没有对它进行测试,但它在我脑海中起作用:

private bool closingForm = false;

// Note, this is "Closing" event handler, not "Close"
Form1_Closing(object sender, CancelEventArgs e)
{
    if (!closingForm && MessageBox.Show("You sure?", "Form1", MessageBoxButtons.YesNo) == DialogResult.No)
    {
        e.Cancel = true;
    }
    else if (backgroundWorker1.IsBusy)
    {
        e.Cancel = true;
        closingForm = true;
        if (!backgroundWorker1.CancellationPending)
            backgroundWorker1.CancelAsync();
    }
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (closingForm)
        this.Close();
}

backgroundWorker1_DoWork中,如果backgroundWorker1.CancellationPendingtrue,则必须检查循环,然后将e.Cancel设置为true,并return来自方法。

无论如何都很少解释。用户启动关闭,因此表单启动后台线程的取消。但实际结束时间较晚,只有这样我们才会关闭表单,小心不要第二次干扰ClosingClose事件。

答案 1 :(得分:1)

您可以在使用之前检查对象是否已被处理

void RecalcTextPos()
{
    if (this.IsDisposed == true)
        return;
    if (string.IsNullOrEmpty(base.Text))
        return;

    using (var graphics = Graphics.FromHwnd(this.Handle))
    {
        TextSize = graphics.MeasureString(base.Text, this.Font);
        TextPos.X = (this.Width / 2) - (TextSize.Width / 2);
        TextPos.Y = (this.Height / 2) - (TextSize.Height / 2);
    }
}