交叉线程问题:已处理无效操作异常

本文关键字:处理 无效 操作 异常 线程 问题 | 更新日期: 2023-09-27 18:04:16

我正在制作一个程序,从txt文件中导入电子邮件并向他们发送消息,但我面临一个问题,目前我正在使用发送邮件方法的线程来防止程序停止响应,确切的问题标题是:

已处理无效操作异常>>>跨线程操作无效:控制'richTextBox1'从创建它的线程以外的线程访问。

代码

    int success = 0;
    int failed = 0;
    int total = 0;
    bool IsRunning;
List<string> list = new List<string>();
private void addmails()
    {
        string path = textBox2.Text;
        foreach (string line in File.ReadAllLines(path))
        {
            list.Add(line);
        }
        IsRunning = true;
    }
    private void sendmails(object sender, DoWorkEventArgs e)
    {
        if (IsRunning == true)
        {
            if (checkBox1.Checked != true)
            {
                SmtpClient client = new SmtpClient(comboBox1.Text);
                client.Credentials = new NetworkCredential(textBox6.Text, textBox7.Text);
                MailMessage message = new MailMessage();
                message.From = new MailAddress(textBox3.Text, textBox1.Text);
                message.Subject = textBox4.Text;
                //message.Body = richTextBox1.Text;
                if (textBox5.Text != "")
                {
                    message.Attachments.Add(new Attachment(textBox5.Text));
                }
                foreach (string eachmail in list)
                {
                    if (IsRunning == true)
                    {
                        try
                        {
                            message.To.Add(eachmail);
                            client.Send(message);
                            listBox1.Items.Add("Successfully sent the message to  : " + eachmail);
                            success++;
                        }
                        catch
                        {
                            listBox1.Items.Add("Failed to send the message to  : " + eachmail);
                            failed++;
                        }
                        message.To.Clear();
                        total++;
                        Thread.Sleep(15);
                        label18.Text = total.ToString();
                        label19.Text = success.ToString();
                        label21.Text = failed.ToString();
                    }
                    else
                    {
                        break;
                    }
                }
                IsRunning = false;
                button3.Text = "Send";
            }
        }
    }
    private void button3_Click(object sender, EventArgs e)
    {
        if (button3.Text == "Send")
        {
            tabControl1.SelectedTab = tabPage3;
            button3.Text = "Stop";
            addmails();
           // IsRunning = true;
            Thread t2 = new Thread(sendmails); // when using that thread i get a cross threading error
            t2.Start();
        }
        else
        {
            IsRunning = false;
            button3.Text = "Send";
            MessageBox.Show("Sending Mails Operation has been terminated","Abort",MessageBoxButtons.OK,MessageBoxIcon.Information);
        }

交叉线程问题:已处理无效操作异常

问题是UI (WinForms)控件正在从非UI线程(新生成的一个)访问

不要那样做。根据具体需要,可以使用Control.Invoke/BeginInvokeSynchronizationContext.Post/Send。使用上面的关键字查找这个问题将导致许多例子。

另一种选择是使用BackgroundWorker对象和RunWorkCompleted/ProgressChanged事件,并且在发送任何内容之前传递所需的所有信息。(例如,不要在新线程中"读取"UI。)

幸福的编码。


参考资料/附加阅读:

如何从另一个线程更改ComboBox数据?-调用/BeginInvoke的小例子,包括一个漂亮的小包装。这些都是解决文章中问题所需要的。

怎么用BeginInvoke?-讨论了WinForms控件访问的限制,windows消息传递如何工作,以及Invoke/BeginInvoke的使用。

Understand SynchronizationContext -与上一个类似,但重点是使用SC而不是Invoke/BeginInvoke。只在前面的链接后阅读。

BackgroundWorker和SynchronizationContext -我更喜欢使用SynchronicationContext而不是Control。Invoke/BeginInvoke(我更喜欢这个接口,不需要检查InvokedRequired或类似的)。本文在BGW的背景下讨论SC。从2005年开始,它使用旧的"委托"语法。如果使用SC,请确保使用从UI线程获得的SC 。参见为什么是SynchronizationContext。目前零?在我的应用程序中,我经常使用静态Send/Post包装器方法来处理所有细节。

你正在使用访问来自不同线程的UI成员,而不是创建它们的线程,当你想从另一个线程访问control成员或方法时,你必须使用Control.Invoke。所以,要让它工作,你必须"我真的不会这样做,只是回答你的问题":

if (IsRunning == true)
{
    bool checkbox1Checked;
    string textBox6Text;
    string textBox7Text;
    string textBox3Text;
    string textBox1Text;
    string textBox4Text;
    string richTextBox1Text;
    string textBox5Text;
    MethodInvoker getValues = new MethodInvoker(delegate()
    {
        checkbox1Checked = checkbox1.Checked;
        textBox6Text = textBox6.Text;
        textBox7Text = textBox7.Text;
        textBox3Text = textBox3.Text;
        textBox1Text = textBox1.Text;
        textBox4Text = textBox4.Text;
        richTextBox1Text = richTextBox1.Text;
        textBox5Text = textBox5.Text;
    });
    if (this.InvokeRequired)
    {
        this.Invoke(getValues);
    }
    else
    {
        getValues();
    }
    if (checkBox1Checked != true)
    {
        SmtpClient client = new SmtpClient(comboBox1Text);
        client.Credentials = new NetworkCredential(textBox6Text, textBox7Text);
        MailMessage message = new MailMessage();
        message.From = new MailAddress(textBox3Text, textBox1Text);
        message.Subject = textBox4Text;
        //message.Body = richTextBox1Text;
        if (textBox5Text != "")
        {
            message.Attachments.Add(new Attachment(textBox5Text));
        }
        foreach (string eachmail in list)
        {
            if (IsRunning == true)
            {
                try
                {
                    message.To.Add(eachmail);
                    client.Send(message);
                    MethodInvoker addToListBox = new MethodInvoker(delegate()
                    {
                        listBox1.Items.Add("Successfully sent the message to  : " + eachmail);
                    }); 
                    if (listBox1.InvokeRequired)
                    {
                        listBox1.Invoke(addToListBox);
                    }
                    else
                    {
                        addToListBox();
                    }
                    success++;
                }
                catch
                {
                    MethodInvoker addToListBox = new MethodInvoker(delegate()
                    {
                        listBox1.Items.Add("Failed to send the message to  : " + eachmail);
                    });
                    if (listBox1.InvokeRequired)
                    {
                        listBox1.Invoke(addToListBox);
                    }
                    else
                    {
                        addToListBox();
                    }
                    failed++;
                }
                message.To.Clear();
                total++;
                Thread.Sleep(15);
                MethodInvoker updateSatatus = new MethodInvoker(delegate()
                {
                    label18.Text = total.ToString();
                    label19.Text = success.ToString();
                    label21.Text = failed.ToString();
                });
                if (this.InvokeRequired)
                {
                    this.Invoke(updateSatatus);
                }
                else
                {
                    updateSatatus();
                }
            }
            else
            {
                break;
            }
        }
        IsRunning = false;
        if (button3.InvokeRequired)
        {
            button3.Invoke(new MethodInvoker(delegate() { button3.Text = "Send"; } ));
        }
        else
        {
            button3.Text = "Send";
        }
    }
}