-2

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);
                    }
                }
Community
  • 1
  • 1
giaosudau
  • 2,211
  • 6
  • 33
  • 64
  • 6
    'but something wrong in my code': It's always good to indicate what you believe is wrong / why you think it's wrong / any errors that you're getting. – forsvarir May 16 '11 at 10:24
  • Something is wrong ... ? , how would any body on earth know what is the problem you are facing .. state the problem – TalentTuner May 16 '11 at 10:27
  • @forsvarir: because have error cross-threads :| – giaosudau May 16 '11 at 10:28
  • 1
    **Please don't ask the [same question](http://stackoverflow.com/questions/5999625/cross-thread-problem) twice.** If you want to improve, revise, or add-to the original question, you should *edit* it, instead. – Cody Gray - on strike May 16 '11 at 10:36
  • @Cody Gray: that is new code and last question is not solved – giaosudau May 16 '11 at 10:38
  • It's exactly the same problem, new code or not. And that's exactly my point: if you want to post new code samples, please edit your old question instead of opening a new one. – Cody Gray - on strike May 16 '11 at 10:43

2 Answers2

4

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))

Stecya
  • 22,896
  • 10
  • 72
  • 102
2

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

TalentTuner
  • 17,262
  • 5
  • 38
  • 63