asp.net ThreadPool - 长时间运行的操作

发布于 2024-08-19 08:35:10 字数 853 浏览 2 评论 0原文

我的应用程序是在 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);
    }
 }

在本地(在我的开发机器上)测试此代码时,我发现应用程序的运行速度要慢得多。

  1. 有更好的方法来编写此代码吗?
  2. 我应该使用 ThreadPool.QueueUserWorkItem 或使用 Thread t = new Thread(new ThreadStart(DoWork)); 创建一个新线程?
  3. 创建一个完全独立的应用程序来发送新闻通讯会更好吗?如果我在同一台机器上运行这个应用程序,这会有帮助吗?

我在这里看到过其他帖子谈论 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.

  1. Is there a better way to write this code?
  2. Should I use ThreadPool.QueueUserWorkItem or create a new thread using Thread t = new Thread(new ThreadStart(DoWork)); ?
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

失去的东西太少 2024-08-26 08:35:10

按照优先顺序:

  1. 创建另一个应用程序。 Windows 服务将是一个不错的选择
  2. 使用 Thread t = new Thread(new ThreadStart(DoWork));
  3. 您当前的实现

In order of preference:

  1. Create another application. Windows service would be a good choice
  2. Use Thread t = new Thread(new ThreadStart(DoWork));
  3. Your current implementation
淤浪 2024-08-26 08:35:10

退出 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?

岁月无声 2024-08-26 08:35:10

是的,我认为您应该将其移出您的网络应用程序,因为您正在从线程池中占用满足您的请求所需的线程(听起来您获得了大量的流量)。

您还同步发送电子邮件,这意味着线程的使用时间比需要的时间长得多 - 如果您继续使用此 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文