我有一個問題,當我把這個線程。 我的想法是當你選擇文件夾郵件 - 當Func1完成時做Func1是MessageCollections() 然後下載郵件文件夾 - Func2是SaveMail() 當下載完成一個郵件然後添加項目到列表視圖 - Func3是AddMesToMailList() 但我的代碼有問題,你能幫助我嗎? 錯誤是:跨線程線MailTree.SelectedNode.Text
private delegate void SaveMailDelegate();
private void AddMesToMailList(IAsyncResult ia)
{
ListViewItem item = new ListViewItem();
Mime m = EncodingMail(MailTree.SelectedNode.Text, mes);
item.Text = mes.MessageUid.ToString();
item.SubItems.Add(m.MainEntity.Subject);
ReturnMime(m);
if (mailfromname != null)
item.SubItems.Add(mailfromname);
else item.SubItems.Add(mailfrom);
item.SubItems.Add(m.MainEntity.Date.ToString());
item.SubItems.Add(mailfrom);
MailList.Items.Add(item);
}
private delegate ImapX.MessageCollection SelectMailFolder(string foldername);
ImapX.MessageCollection MessageCollections(string foldername)
{
return messages=client.Folders[foldername].Search("ALL", false);
}
public void SaveMail()
{
string path1= "D:\\" ;
string path2=Username;
string path3=MailTree.SelectedNode.Text;
string path4 = mes.MessageUid.ToString() + ".eml";
// @"D:\" + Username + "\\" + MailTree.SelectedNode.Text + "\\" + mes.MessageUid.ToString() + ".eml")\
string path=Path.Combine(path1,path2,path3,path4);
string savepath = Path.Combine(path1, path2, path3);
if (!File.Exists(path))
{
mes.Process();
mes.SaveAsEmlToFile(savepath, mes.MessageUid.ToString()); //Store messages to a Location
}
}
private void MailTree_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
MailList.Items.Clear();
for (int i = 0; i < client.Folders.Count; i++)
{
(ContextMenuListView.Items[1] as ToolStripMenuItem).DropDownItems[i].Click += new EventHandler(MainForm_Click);
}
if (MailTree.SelectedNode.Text == Username)
{
webBrowser1.Visible = false;//webBrowser1.DocumentText = "Hello Baby";
AttachmentList.Visible = false;
groupBox1.Visible = false;
}
else
{
webBrowser1.Visible = true;
groupBox1.Visible = true;
string select = MailTree.SelectedNode.Text;
//messages = client.Folders[select].Search("ALL", false);// Search mail in your choossen Folder
SelectMailFolder se = null;
se += new SelectMailFolder(MessageCollections);
se.BeginInvoke(select,new AsyncCallback(NewMethod), null);
}
}
private void NewMethod(IAsyncResult ias)
{
AmoutOfMail = messages.Count(); //Amout of Mail in this Folder
for (int i = 0; i < AmoutOfMail; i++)
{
mes = messages[i];
SaveMailDelegate del = null;
del += new SaveMailDelegate(SaveMail);
IAsyncResult ia = del.BeginInvoke(new AsyncCallback(AddMesToMailList), null);
}
}
「但蹊蹺的在我的代碼」:這是一件好事,說明你認爲什麼是錯的/你爲什麼認爲這是錯誤/你得到任何錯誤。 – forsvarir 2011-05-16 10:24:40
有什麼不對...? ,地球上的任何人如何知道你面臨的問題是什麼?陳述問題 – TalentTuner 2011-05-16 10:27:02
@forsvarir:因爲有錯誤的交叉線程: – giaosudau 2011-05-16 10:28:05