Cross thread problem?

后端 未结 3 1033
后悔当初
后悔当初 2020-12-22 02:19

My error

Cross-thread operation not valid: Control \'MailTree\' accessed from a thread other than the thread it was created on.

3条回答
  •  感情败类
    2020-12-22 02:55

    I think AddMesToMailList() is trying to modify the view elements but it is on a wrong thread.

    Try something like this

    void AddMesToMailList()
    {
        if (this.InvokeRequired)
        {
             this.BeginInvoke(new Action(AddMesToMailList));
             return;
        }
    
        // do stuff that original  AddMesToMailList() did.
    }
    

    EDIT: SaveMail is a little complicated as it has a return value but you can try this

    public int SaveMail(ImapX.Message mess)
    {
         if(this.InvokeRequired)
         {
                return (int) this.Invoke(
                       new Func( m => SaveMail(mess)) );
         }
         else
         {
    
            if (!File.Exists(@"D:\" + Username + "\\" + MailTree.SelectedNode.Text + "\\" + mes.MessageUid.ToString() + ".eml"))
            {
                mess.Process();
                mess.SaveAsEmlToFile(@"D:\" + Username + "\\" + MailTree.SelectedNode.Text + "\\", mes.MessageUid.ToString());   //Store messages to a Location 
    
            }
           // mes.MessageUid=mess.MessageUid;
            return 1;  
    
         }
    
    
    
    }
    

提交回复
热议问题