0

In My C# windows application i have built one automated mail system to inform the user about the key events they set in the application using SMTP Setting...this mailing system is working fine. I have used System.Net.Mail namespace to achieve this....

My Problem is that whenever the mail is sent successfully, I need to display a notification window(Popup window) displaying name of Recepient and his email ID on one windows form.

For this purpose i'm using secondary thread, but it is not working properly, it is making my main application non responsive till it delivers mail to all users. For each Mail have given thread sleep of 5 Secs, consider there are 10 mails, then I will not be able to use my main application for 50 seconds....So any help would be highly appreciated...

I have done C# coding like this:

    //My main Function
    public void SendMail(dataset ds)
    {
      for(inti=0;i<ds.Tables[0].Rows.Count;i++)
      {
       string resValue = genFunc.SMTPClienTSetting(ds, mail);
        if (resValue == "True")
        {
            Thread tPopUp = new Thread(new ThreadStart(this.ShowMessagePopUp));
            tPopUp.IsBackground = true;
            tPopUp.Start();
            lblPOpUpInfo = "Message sent successfully";
        }
        else
        {
            Thread tPopUp = new Thread(new ThreadStart(this.ShowMessagePopUp));
            tPopUp.IsBackground = true;
            tPopUp.Start();
            lblPOpUpInfo = "Message sending failed";
        }
       }
    }

    //Thread Function to open secondary form
    public void ShowMessagePopUp()
    {
        try
        {
            frmHomePopUp homePopUp = new frmHomePopUp();
            homePopUp.Show();
            homePopUp.lblInfo.Text = lblPOpUpInfo;
            Application.DoEvents();
            Thread.Sleep(5000);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

public string SMTPClienTSetting(DataSet ds, MailMessage MailstoSend)
        {
            try
            {
                SmtpClient objsmtp = new SmtpClient();
                NetworkCredential NetAccess = new NetworkCredential();

                NetAccess.UserName = ds.Tables[0].Rows[0][0].ToString();
                NetAccess.Password = ds.Tables[0].Rows[0][1].ToString();
                objsmtp.Credentials = NetAccess;
                objsmtp.Host = ds.Tables[0].Rows[0][2].ToString();
                objsmtp.Port = Convert.ToInt16(ds.Tables[0].Rows[0][3].ToString());

                objsmtp.Send(MailstoSend);

                System.Threading.Thread.Sleep(500);

                return "True";
            }
            catch (NullReferenceException nr)
            {
                return nr.Message;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

I'm able to show my form using using thread, but main application is not responding till it finishes sending mails....

Vishal I P
  • 2,005
  • 4
  • 24
  • 41

3 Answers3

2

You can use TPL for asynchronous mail send and ContinueWith for interaction with UI thread. Something like that

for (int i = 0; i < 10; i++)
{
    var asyncTask = Task.Factory.StartNew(() => SendMail(i)).ContinueWith((result) =>
    {
        // UI interaction logic
    });
}
private bool SendMail(string data)
{
    // send logic
    // you can handle result from this function in ContinueWith delegate function
}
vadim
  • 1,698
  • 1
  • 10
  • 19
  • For new changes to appear they should reach main UI thread before. So to achieve this you have to use `SynchronizationContext` with `ContinueWith` – vadim Jan 03 '13 at 05:22
0

I don´t see the need for a new thread here. Also, avoid using DoEvents, see here or search for yourself. Have you considered making a small Listbox as some sort of console/log and display success or failure messages there? Every user hates applications which throw around messageboxes all the time.

Community
  • 1
  • 1
Jobo
  • 1,084
  • 7
  • 14
0

Your blocking code actually is contained in SMTPClienTSetting method which, in your case, is executed on the main thread (UI thread). For avoiding UI blocking you should execute this method on different thread (you can use the approach suggested by Vadim).

Ion Sapoval
  • 635
  • 5
  • 8