asp.net ThreadPool - 长时间运行的操作
我的应用程序是在 iis 6 (windows 2003) 上运行的 asp.net 3.5 该应用程序每天为 1000 名用户提供服务(100-500 名在线用户)。
我想每周向客户发送一封电子邮件通讯。
每次大约 200,000 封电子邮件。
这是我正在使用的代码:
ThreadPool.QueueUserWorkItem(new WaitCallback(AsyncProcessMailerQueue), null);
private static void AsyncProcessMailerQueue(object data)
{
for (int i=0;i<users.count ; i++)
{
MailMessage message = new MailMessage();
.......
SmtpClient smtpClient = new SmtpClient();
smtpClient.Send(message);
}
}
在本地(在我的开发机器上)测试此代码时,我发现应用程序的运行速度要慢得多。
- 有更好的方法来编写此代码吗?
- 我应该使用 ThreadPool.QueueUserWorkItem 或使用 Thread t = new Thread(new ThreadStart(DoWork)); 创建一个新线程?
- 创建一个完全独立的应用程序来发送新闻通讯会更好吗?如果我在同一台机器上运行这个应用程序,这会有帮助吗?
我在这里看到过其他帖子谈论 ThreadPool 与 Thread,但似乎没有人确定哪个更好。
My application is a asp.net 3.5 running on iis 6 (windows 2003)
This application is serving 1000's of users daily (100-500 users online).
I want to send an email newsletter to customers weekly.
Around 200,000 emails every time.
This is the code I'm using:
ThreadPool.QueueUserWorkItem(new WaitCallback(AsyncProcessMailerQueue), null);
private static void AsyncProcessMailerQueue(object data)
{
for (int i=0;i<users.count ; i++)
{
MailMessage message = new MailMessage();
.......
SmtpClient smtpClient = new SmtpClient();
smtpClient.Send(message);
}
}
When testing this locally (on my dev machine) I see the application is working a lot slower.
- Is there a better way to write this code?
- Should I use ThreadPool.QueueUserWorkItem or create a new thread using Thread t = new Thread(new ThreadStart(DoWork)); ?
- Will it be better to create a totally separate application for the purpose of sending the newsletters. will that help if ill run this application on the same machine?
I've seen other posts here talking about ThreadPool vs Thread but its seem no one is sure which is better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
按照优先顺序:
In order of preference:
Thread t = new Thread(new ThreadStart(DoWork));
退出 asp.net 将是一个不错的选择。这可能是您从命令提示符运行的简单命令行应用程序。为什么需要服务或将其托管为 URL?
Moving out of asp.net would be a good choice. This could be a simple command line app you run from a command prompt. Why do you need a service or it to be hosted as a URL?
是的,我认为您应该将其移出您的网络应用程序,因为您正在从线程池中占用满足您的请求所需的线程(听起来您获得了大量的流量)。
您还同步发送电子邮件,这意味着线程的使用时间比需要的时间长得多 - 如果您继续使用此 ThreadPool 方法,我建议将它们排队到 IIS SMTP 服务(查看 System.Net.Mail)。 SmtpClient.DeliveryMethod),它只是将文件写入队列文件夹,该文件夹由 IIS SMTP 服务监控。
但实际上你应该考虑将其转移到 Windows 服务。
Yes I think you should move this out of your web app, as you're taking up threads from the thread pool that are needed to serve your requests (sounds like you get a good amount of traffic).
You're also sending the emails syncronously which means the thread is being used for a lot longer than it need be - if you continue using this ThreadPool approach I would suggest queuing them to the IIS SMTP service (look at System.Net.Mail.SmtpClient.DeliveryMethod) which just writes a file to a queue folder, which is monitored by the IIS SMTP service.
But really you should consider moving this to a windows service.