跨线程问题? C#
可能的重复:
跨线程问题?
当我调用这个线程时遇到问题。 我的想法是当您选择文件夹邮件时 - do Func1 is MessageCollections() 当 Func1 完成后,将邮件下载到文件夹中 - 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);
}
}
Possible Duplicate:
Cross thread problem?
I have a problem when I call this thread.
My idea is when you select folder mail - do Func1 is MessageCollections()
when Func1 finish then Download Mail in Folder - Func2 is SaveMail()
when download finish one mail then add item to listview - Func3 is AddMesToMailList()
but something wrong in my code can you help me?
Error is: cross-threads in line 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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在单独的线程中调用
AddMesToMailList
。您无法与另一个线程中的 Gui 元素交互。使用Invoke切换到Gui线程this.Invoke(new Action(AddMesToMailList))
You are calling
AddMesToMailList
in seperate thread. You cant interact with Gui elemnents from another thread. Use Invoke to switch to Gui threadthis.Invoke(new Action(AddMesToMailList))
根据我的疯狂猜测,您可能会遇到跨线程控制访问问题,当您从主 UI 线程以外的另一个线程触摸任何 UI 控件时,就会发生这种情况。
From my wild wild guess , you may be getting cross thread control access probelm , that occures when you touch any UI control from another thread other than main UI thread