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.In stance.Get Property(P ropertyDat a.PROP_SMT P_SERVER)) ;
Then I try and send it in a thread
Thread tEmail = new Thread(new ThreadStart(client.Send(ms g)));
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
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.In
Then I try and send it in a thread
Thread tEmail = new Thread(new ThreadStart(client.Send(ms
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
I think the method which you want to execute when thread start should be parameter less.
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();
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");
}
}
}
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.
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.
Open in new window