Sending email in it's own Thread.

bowemc
bowemc used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Change that one line to this

Thread tEmail = new Thread(new ThreadStart(delegate { client.Send(msg); }));
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

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial