如何解决outofrange异常?

时间:2016-05-21 22:00:34

标签: c# .net winforms

在backgroundworker dowork事件中

private int numberofallmessages = 0;
        private int countMsg = 0;
        private int nProgress = 0;
        private HashSet<string> downloaded = new HashSet<string>();

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            using (var client = new Pop3Client())
            {
                client.Connect(textServer.Text, Convert.ToInt32(textPort.Text), ssl);
                client.AuthenticationMechanisms.Remove("XOAUTH2");
                client.Authenticate(textUser.Text, textPassword.Text);

                if (client.Capabilities.HasFlag(Pop3Capabilities.UIDL))
                {
                    string uidsSupported = "";
                }

                var uids = client.GetMessageUids();

                for (int i = 0; i < client.Count; i++)
                {
                    if (backgroundWorker1.CancellationPending == true)
                    {
                        e.Cancel = true;
                        return;
                    }
                    string currentUidOnServer = uids[i];
                    if (!downloaded.Contains(uids[i]))
                    {
                        allMessages.Add(client.GetMessage(i));
                        downloaded.Add(uids[i]);
                        SaveFullMessage(client.GetMessage(i), i);
                        w = new StreamWriter(emailsIDSFile, true);
                        w.WriteLine(currentUidOnServer);
                        w.Close();
                        nProgress = (client.Count - i + 1) * 100 / client.Count;
                        backgroundWorker1.ReportProgress(nProgress, client.Count.ToString() + "/" + i);
                    }
                        nProgress = (client.Count - i + 1) * 100 / client.Count;
                        backgroundWorker1.ReportProgress(nProgress, client.Count.ToString() + "/" + i);
                }

                client.Disconnect(true);
            }
         }

例外情况是progresschanged事件:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            pbt.Value = e.ProgressPercentage;
            pbt.Text = e.ProgressPercentage.ToString() + "%";
            pbt.Invalidate();
            label8.Text = e.UserState.ToString();
            label8.Visible = true;
            ListViewCostumControl.lvnf.Items.Add(new ListViewItem(new string[]
            {
              allMessages[countMsg].From.ToString(),
              allMessages[countMsg].Subject,
              allMessages[countMsg].Date.ToString() 
            }));
            countMsg += 1;
        }

例外是:

ListViewCostumControl.lvnf.Items.Add(new ListViewItem(new string[]
                {
                  allMessages[countMsg].From.ToString(),
                  allMessages[countMsg].Subject,
                  allMessages[countMsg].Date.ToString() 
                }));

在这种情况下,allMessages和countMsg都具有值1。 所以这就是它抛出异常的原因,但我该如何解决?

也许这一行:

countMsg += 1;

应该在另一个地方?也许在dowork事件中?

1 个答案:

答案 0 :(得分:0)

你不应该countMsg超过allMessages中的项目总数,但是在递增之前你没有检查。您可以将代码放在for循环中,循环次数与allMessages中的元素相同。或者你可以检查是否为空,如果你得到一个

就停止