c# - SMTP client StackOverflowException -


i'm creating massmailing program basically, , problem if without threads, freeze , needs able show user progress. without using class/object works fine. stackoverflowexception:

{cannot evaluate expression because current thread in stack overflow state.} 

on

this.numericupdown1 = new system.windows.forms.numericupdown(); 

notice: can missleading because haven't created thread yet still get's stackoverflowexception.

this code:

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);             (int = 0; < 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();             }           }     }  } 

i'm going create thread of dowork. have never done , know code pretty bad @ moment. kind of highly appreciated. i'm new c# , multi-threading @ all. form2 consists of textboxes etc contain smtp settings, no code there.

edit: if don't create new object

worker work = new worker(); 

it works fine!

in form create new worker:

worker work = new worker(); 

but when create worker create new form:

form1 frm1 = new form1(); 

and goes on , on until breaks.

remove worker work = new worker(); form 1. change form 1 constructor this:

worker _worker; public form1() {     initializecomponent();     _worker = new worker(this); } 

change worker this:

public class worker {     form1 _form1;     public worker(form1 form1)     {         _form1 = form1;     }     ... 

and remove form1 frm1 = new form1() worker.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -