In a backgroundworker dowork event
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);
}
}
And the exception is in the progresschanged event:
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;
}
The exception is on the part:
ListViewCostumControl.lvnf.Items.Add(new ListViewItem(new string[]
{
allMessages[countMsg].From.ToString(),
allMessages[countMsg].Subject,
allMessages[countMsg].Date.ToString()
}));
In this case allMessages and countMsg both have the value 1. So this is why it's throwing the exception but how should i solve it ?
Maybe this line:
countMsg += 1;
Should be in another place ? Maybe in the dowork event ?