开发者

Cross thread problem?

My error

开发者_JS百科

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

with my code My idea is when SaveMail method has finish store 1 mes then add this mes to listview.

private delegate int SaveMailDelegate(ImapX.Message mes);

public int SaveMail(ImapX.Message mess)
{
    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;            
}
Mime EncodingMail(string NodeName,string focusitem)
{
    Mime m = new Mime();
    m=Mime.Parse("D:\\" + Username+ "\\"+NodeName+"\\"+focusitem+".eml");
    return m;
}
private void AddMesToMailList()
{
    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 void SaveMailDone(IAsyncResult iar)
{
    SaveMailDelegate del = iar.AsyncState as SaveMailDelegate;
    if (del != null)
    {
        int result = del.EndInvoke(iar);
        AddMesToMailList();            
    }
}    

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;

        try
        {
            messages = client.Folders[MailTree.SelectedNode.Text].Search("ALL", false); // Search mail in your choossen Folder
            AmoutOfMail = messages.Count(); //Amout of Mail in this Folder                                         
            for (int i = 0; i < AmoutOfMail; i++)
            {
                mes=messages[i];
                SaveMailDelegate del = new SaveMailDelegate(this.SaveMail);
                del.BeginInvoke(mes, new AsyncCallback(this.SaveMailDone), del);                                      
            }
        }
        catch (Exception)
        { }
    }
}


You cannot directly access a control from another thread, you will have to invoke it.

private delegate void ControlCallback(string s);

public void CallControlMethod(string text)
{
    if (control.InvokeRequired)
    {
        ControlCallback call = new ControlCallback((s) =>
        {
            // do control stuff
        });
        control.Invoke(call, new object[] { text });
    }
    else
    {
        // do control stuff
    }
}


you can't access the UI on a different thread than what it was created on. From inside your secondary thread (the one that runs your callback handler) you will need to call Form.BeginInvoke to register a method that will be run on the UI thread. From that method you can update your UI controls


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<ImapX.Message, int>( 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;  

     }



}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜