如何对这项工作进行多线程处理?

发布于 2024-11-06 12:35:45 字数 1963 浏览 1 评论 0原文

我的想法是,如果完成下载此消息,然后将它们添加到 ListView 中。但如果这段代码我们将等待很长时间并且程序将不会响应。你能帮助我吗?

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];

                SaveMail(mes); // dowload mes and store message

                ListViewItem item = new ListViewItem();
                Mime m = EncodingMail(MailTree.SelectedNode.Text, mes); // read mes from place store message
                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);
            }
        }                                         
        catch (Exception)
        { }                                                
    }
}

My idea is if finished download this messages then add them to ListView. But if this code we will waiting very long and program will be not responding. Can you help me?

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];

                SaveMail(mes); // dowload mes and store message

                ListViewItem item = new ListViewItem();
                Mime m = EncodingMail(MailTree.SelectedNode.Text, mes); // read mes from place store message
                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);
            }
        }                                         
        catch (Exception)
        { }                                                
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

羁客 2024-11-13 12:35:45

是的。

查看任务以卸载任何内容你想进入不同的线程。

只需确保您使用 控件即可。 InvokeRequired 将回调代码放回 UI 线程。您永远不应该从与 UI 线程不同的线程修改 UI。

Yes.

Look into the Tasks library to offload whatever you want onto a different thread.

Just make sure that you use Controls.InvokeRequired to place the callback code back onto the UI thread. You should never modify the UI from a different thread than the UI thread.

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