Link to home
Start Free TrialLog in
Avatar of bowemc
bowemc

asked on

Sending email in it's own Thread.

Hi,

In my code I am trying to send an email in it's own thread.

I create a SmtpClient object

SmtpClient client = new SmtpClient(PropertyData.Instance.GetProperty(PropertyData.PROP_SMTP_SERVER));

Then I try and send it in a thread

Thread tEmail = new Thread(new ThreadStart(client.Send(msg)));
                tEmail.IsBackground = true;
                tEmail.Start();

However I get an error saying ThreadStart expects a method ....am I not providing the Send method of the SmtpClient  object?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of mikeada
mikeada
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If c# 3.0 try:


Thread tEmail = new Thread(new ThreadStart(() => client.Send(msg)));
                tEmail.IsBackground = true;
                tEmail.Start();

Open in new window

Hi,

I think the method which you want to execute when thread start should be parameter less.

Thread tEmail = new Thread(new ThreadStart(client.Send()));
                tEmail.IsBackground = true;
                tEmail.Start();

Open in new window

Please check sample code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Threading;
 
namespace WindowsService1
{
  public partial class Service1 : ServiceBase
  {
    Thread MyThread;
    BatchProcessor Process;
    
 
    public Service1()
    {
      InitializeComponent();
    }
 
    protected override void OnStart(string[] args)
    {
      // TODO: Add code here to start your service.
      MyThread = new Thread(Process.Start);
      MyThread.Start();
    }
 
    protected override void OnStop()
    {
      // TODO: Add code here to perform any tear-down necessary to stop your service.
      Process.Abort();
      MyThread.Abort();
    }
  }
}
 
 
//---------------------MAIN PROCESSING CLASS--------------------------
 
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
namespace WindowsService1
{
  class BatchProcessor
  {
 
    System.Timers.Timer mTmr;
 
    public void BatchProcessor()
    {
      mTmr = new System.Timers.Timer();
      mTmr.Elapsed += new System.Timers.ElapsedEventHandler(mTmr_Elapsed);
    }
 
    void mTmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
      MainProcess();
    }
 
    public void Start()
    {
      Console.WriteLine("Start");
      mTmr.Interval = 20;
      mTmr.Enabled = true;
      MainProcess();
    }
 
    public void Abort()
    {
      Console.WriteLine("Abort");
    }
 
    public void MainProcess()
    {
      Console.WriteLine("Main Process");
    }
 
  }
}

Open in new window

bhagwantsingh:
You're right that the method should be parameter-less.  If you look at my solution above, by passing the anonymous delegate, you would be passing a parameter-less method into the threadstart, but client.Send(msg) has to send the message, hence the delegate wrapper.

To the Author, I tested that solution yesterday.