Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

c# - Cross thread problem?

My error

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)
        { }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...