Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net ThreadPool - long running operation

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.

like image 498
RuSh Avatar asked Jan 27 '10 17:01

RuSh


People also ask

How ASP Net core is high performance?

ASP.NET Core apps should be designed to process many requests simultaneously. Asynchronous APIs allow a small pool of threads to handle thousands of concurrent requests by not waiting on blocking calls. Rather than waiting on a long-running synchronous task to complete, the thread can work on another request.

How can I improve my net core application performance?

Optimize Data Access Here are some techniques that you can use to increase app performance. Reduce the number of HTTP calls i.e. the number of network round trips. Instead of making multiple calls to the server, try fetching the data in one or two calls. Set a cache for unchanged data.

Does Task run Use the ThreadPool?

By default, TPL types like Task and Task<TResult> use thread pool threads to run tasks. You can also use the thread pool by calling ThreadPool.


2 Answers

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
like image 153
Darin Dimitrov Avatar answered Sep 28 '22 05:09

Darin Dimitrov


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?

like image 40
No Refunds No Returns Avatar answered Sep 28 '22 04:09

No Refunds No Returns