跨线程问题? C#

发布于 2024-11-07 15:22:56 字数 4352 浏览 0 评论 0原文

可能的重复:
跨线程问题?

当我调用这个线程时遇到问题。 我的想法是当您选择文件夹邮件时 - 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葮薆情 2024-11-14 15:22:56

您正在单独的线程中调用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 thread

this.Invoke(new Action(AddMesToMailList))

故事与诗 2024-11-14 15:22:56

根据我的疯狂猜测,您可能会遇到跨线程控制访问问题,当您从主 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文