我正在关闭我的项目,单击右上角的 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 关闭事件。我应该在事件中放什么?停止和处理什么?
编码比解释更容易。另外,还没有测试过,但它在我脑海中起作用:
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.CancellationPending
为 true
,则必须检查循环,然后设置 e.Cancel
为 true
,然后从方法中返回
。
无论如何,很少解释。用户启动关闭,因此表单启动后台线程的取消。但真正的完成要晚一些,只有到那时我们才会关闭表单,注意不要在第二次时干扰 Closing
和 Close
事件。
我是一名优秀的程序员,十分优秀!