Hi,
I need to send emails asychronously through a console application. I need to do some DB updates on the callback but my application is exiting before the callback code gets run!
How can I stop this from happening in a nice manner rather than simply guessing how long to wait before exiting. I would imagine the Async calls get placed in some form of thread? Is it possible to check if any are waiting to be called?
Sample Code
private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Get the unique identifier for this asynchronous operation.
String token = (string) e.UserState;
if (e.Cancelled)
{
Console.WriteLine("[{0}] Send canceled.", token);
}
if (e.Error != null)
{
Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
}
else
{
// update DB
Console.WriteLine("Message sent.");
}
}
public static void Main(string[] args)
{
var users = Repository.GetUsers();
SmtpClient client = new SmtpClient("Host");
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
MailAddress from = new MailAddress("
[email protected]", "System", Encoding.UTF8);
foreach (var user in users)
{
MailAddress to = new MailAddress(user.Email);
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test";
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "test message 1" + someArrows;
message.SubjectEncoding = System.Text.Encoding.UTF8;
string userState = String.Format("Message for user id {0}", user.ID);
client.SendAsync(message, userState);
message.Dispose();
}
// need to wait here until I have received a callback for each message
// otherwise the application will exit
}