【问题标题】:SMTP client StackOverflowExceptionSMTP 客户端 StackOverflowException
【发布时间】:2015-07-14 12:33:13
【问题描述】:

我基本上是在创建一个群发邮件程序,问题是如果我在没有线程的情况下执行它,它显然会冻结并且它需要能够向用户显示进度。不使用另一个类/对象它工作得很好。 我得到一个stackoverflow异常:

{Cannot evaluate expression because the current thread is in a stack overflow state.}

this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();

注意:这可能会产生误导,因为我什至还没有创建线程,但它仍然会得到 stackoverflowexception。

这是代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Windows;
using System.Net.Mail;

namespace Mass_Mail
{

    public partial class Form1 : Form
    {
        Form2 frm = new Form2();
        Worker work = new Worker();

        public List<String> succed = new List<String>();
        public List<String> failed = new List<String>();

        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            OpenFileDialog openFileDialog1 = new OpenFileDialog(); //Skapa och definiera openfiledialog

            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); //sätt startmappen till skrivbordet
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; //textfiler.
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {


                        textBox1.Text = openFileDialog1.FileName.ToString();
                        using (StreamReader sr = new StreamReader(openFileDialog1.FileName)) {
                            String line;
                            while ((line = sr.ReadLine()) != null)
                            {
                                textBox2.AppendText(line + Environment.NewLine);
                            }

                }
                    }
             catch (Exception ex) {
                 MessageBox.Show("Kunde inte öppna  " + ex.Message);
             }

        }

}

        private void button1_Click(object sender, EventArgs e)
        {


        }


        private void button3_Click(object sender, EventArgs e)
        {
            frm.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            frm.comboBox1.SelectedIndex = 0;
        }

        private void button4_Click(object sender, EventArgs e)
        {
           // work.sendmail(textBox6.Text);
        }



    }
    public class Worker
    {
        public void DoWork()
        {
            string[] lines = frm1.textBox2.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            for (int i = 0; i < lines.Length; i++)
            {
                sendmail(lines[i]);
            }
        }
        Form1 frm1 = new Form1();
        Form2 frm2 = new Form2();
        public void sendmail(String mail)
        {
            try
            {

                SmtpClient client = new SmtpClient(frm2.textBox1.Text, int.Parse(frm2.textBox2.Text));
                MailMessage msg = new MailMessage(frm1.textBox3.Text, mail);
                client.Timeout = decimal.ToInt32(frm2.numericUpDown1.Value);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential("", "");
                switch (frm2.comboBox1.SelectedIndex)
                {
                    case 0:
                        client.EnableSsl = true;
                        break;
                    case 1:
                        client.EnableSsl = false;
                        break;
                }

                msg.Subject = frm1.textBox4.Text;
                msg.Body = frm1.textBox5.Text;
                msg.BodyEncoding = UTF8Encoding.UTF8;
                msg.IsBodyHtml = true;

                client.Send(msg);
                frm1.succed.Add(mail);
                frm1.label6.Text = "Lyckades: " + frm1.succed.Count.ToString();
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                MessageBox.Show("ERROR! " + ex);
                frm1.failed.Add(mail);
                frm1.label7.Text = "Misslyckades: " + frm1.failed.Count.ToString();
            }
            catch (System.FormatException e)
            {
                MessageBox.Show("ERROR! " + e);
                frm1.failed.Add(mail);
                frm1.label7.Text = "Misslyckades: " + frm1.failed.Count.ToString();
            }


        }
    }

}

我要创建一个 DoWork 线程。我从来没有做过这样的事情,我知道目前的代码很糟糕。任何形式的帮助都将受到高度赞赏。 我对 C# 和多线程也很陌生。 Form2 仅包含包含 SMTP 设置的文本框等,那里没有代码。

编辑: 如果我不创建新对象

Worker work = new Worker();

效果很好!

【问题讨论】:

  • 您所说的导致错误的行根本不包含在您的代码示例中
  • 因为不知道是哪一个。错误是在这里引起的:this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();但那是因为线程进入了 stackoverflow 状态。
  • 是的,那么请向我们展示this.numericUpDown1 = new System.Windows.Forms.NumericUpDown(); 周围的代码 - 因为这就是错误所在。
  • 在 Form2.Designer.cs 中,在 InitializeComponent() 中。
  • Form1 frm1 = new Form1(); Form2 frm2 = new Form2(); 在您的工作人员中不会像您期望的那样做。传递对已实例化表单的引用,或使用适当的关注点分离并使用事件/数据绑定。

标签: c# stack-overflow


【解决方案1】:

在您的表单中创建一个新的工作人员:

Worker work = new Worker();

但是当你创建一个工人时,你会创建一个新的表单:

Form1 frm1 = new Form1();

它会一直持续下去,直到它破裂。

移除 Worker 工作 = new Worker();从 Form 1。像这样更改 Form 1 的构造函数:

Worker _worker;
public Form1()
{
    InitializeComponent();
    _worker = new Worker(this);
}

像这样改变你的工人:

public class Worker
{
    Form1 _form1;
    public Worker(Form1 form1)
    {
        _form1 = form1;
    }
    ...

并从您的工作人员中删除 Form1 frm1 = new Form1()。

【讨论】:

    猜你喜欢
    • 2011-03-27
    • 2010-12-10
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    相关资源
    最近更新 更多