SmtpClient.SendAsync - 如何在触发回调之前停止应用程序退出?
我需要通过控制台应用程序异步发送电子邮件。我需要对回调进行一些数据库更新,但我的应用程序在回调代码运行之前就退出了!
我怎样才能以一种好的方式阻止这种情况发生,而不是简单地猜测退出之前需要等待多长时间。我想异步调用会被放置在某种形式的线程中?是否可以检查是否有人正在等待呼叫?
示例代码
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
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建一个ManualResetEvent,在退出之前调用WaitOne 就可以了。当执行最后一次电子邮件/数据库更新时,调用 ManualResetEvent 上的 Set。
Create a ManualResetEvent call WaitOne one it on it before exiting. When the last email/dbupdate is performed, call Set on the ManualResetEvent.