从我的pop3帐户收到我的电子邮件时,progressBar百分比达到99%为什么?

时间:2016-02-15 22:49:42

标签: c# .net winforms

我得到的是99%而在标签8上我看到1而不是0而且progressBar应该达到100%。

这是背景工作者并且已经改变并完成了事件:

private int numberofallmessages = 0;
        private int countMsg = 0;

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            OpenPop.Pop3.Pop3Client PopClient = new OpenPop.Pop3.Pop3Client();
            PopClient.Connect("mail.bezeqint.net", 110, false);
            PopClient.Authenticate("mymeila", "mypass",
                OpenPop.Pop3.AuthenticationMethod.UsernameAndPassword);

            int messageCount = PopClient.GetMessageCount();
            numberofallmessages = messageCount;
            allMessages = new List<OpenPop.Mime.Message>(messageCount);
            for (int i = messageCount; i > 0; i--)
            {
                if (backgroundWorker1.CancellationPending == true)
                {
                    e.Cancel = true;
                    return;
                }
                allMessages.Add(PopClient.GetMessage(i));
                int nProgress = (messageCount - i + 1) * 100 / messageCount;
                backgroundWorker1.ReportProgress(nProgress, PopClient.GetMessageCount().ToString() + "/" + i);
            }
            PopClient.Disconnect();        
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            pbt.Value = e.ProgressPercentage;
            pbt.Text = e.ProgressPercentage.ToString() + "%";
            label8.Text = e.UserState.ToString();
            label8.Visible = true;
            lstMail.Items.Add(allMessages[countMsg].Headers.Subject);
            countMsg += 1;
        }

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

在构造函数中,我启动backgroundworker并将progressBar(pbt)设置为0%。

backgroundWorker1.RunWorkerAsync();
pbt.Text = "0%";

这是progressBar代码: 在form1的顶部:

ProgressBarWithText pbt = new ProgressBarWithText();

在构造函数中:

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

ProgressBarWithText类:

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

            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 (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);
                }
            }

            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);

                if (dontpaint == false)
                {
                    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;
                }
            }
        }

问题是,我是否真的下载了所有电子邮件,还是剩下一封?如何更改/设置代码是否会获得100%和0个下载的电子邮件?

我做了一个测试,现在我更改了一行:

int messageCount = PopClient.GetMessageCount();

int messageCount = 5;

我在listView中看到我向设计师添加了5封电子邮件。 但是我在label8中看到的是7011/1 7011是帐户中的整体电子邮件,但是对于测试我只下载了5.再次它停在1而不是0。 并且progressBar停止在80%,文本是80%,但我看到progressBar中的绿色移动到100%结束。

为什么它会停在1以及为什么我看到80%?

0 个答案:

没有答案