Topics inside foreach loop in C #

Hi guys

I had the following code to send different emails to other users in my asp.net web application.

foreach (DataRow dataRow in dataTable.Rows) { sendMails(); } public void sendMails() { //mail code } 

Now I want to use threads inside the foreach loop, but I don’t know what will be the result, because if I start “n” the number of threads, what will happen to the thread pool. Think my datatable contains 1000 rows,

Is it possible to execute 1000 threads at the same time?

 foreach (DataRow dataRow in dataTable.Rows) { ThreadStart ts1 = new ThreadStart(sendMails); Thread thread1 = new Thread(ts1); thread1.Start(); } public void sendMails() { //mail code } 
+6
multithreading c #
source share
8 answers

Start one thread that will complete the task of sending all letters:

 new Thread(() => { foreach (DataRow dataRow in dataTable.Rows) { sendMails(); } }).Start(); 
+14
source share

Using the code you have, nothing will happen to threadpool - you must create new threads completely outside of threadpool.

Are you really sure you want to use a stream for mail or even multiple streams? I assume that you will be limited by your connection to the local SMTP server, and starting multiple threads will not help.

Starting a single thread to send everything in the background (at Darin’s suggestion) is more reasonable if the purpose of using the streams is to return the page to the user: “Yes, now I'm sending emails”. On the other hand, this means that if this process stops for any reason, you can ultimately send only half of them. An alternative (as suggested by Charlie) would be to use a queuing system (such as a file, database, or MSMQ). Thus, you can block until you queue all the letters, which means that when you return to the user, you can be sure that the data is “safe”, but you can do the actual sending of mail in the background service mode, which could be more reliable.

+5
source share

Wouldn't it be wiser to look at using a message queue with a separate Windows service to send emails?

+4
source share

Do not create your own threads with 1000 threads, this means that the processor spends all of its time switching between them and very little time doing any work.

Use threadpool for this, it will execute up to 25 (default) background threads and can automatically block when they are all busy.

See MSDN Tutorial

+1
source share

It might be wiser to use a thread pool for this, or even Parallel Linq, which comes in .NET 4.0 (or in Parallel FX):

 dataTable.Rows.AsParallel().Select(a => { //mail code return null; }); 
+1
source share

Email subject is not the best idea, as it was explained why many, however, if you decided to create one background thread to process all emails, the thread’s execution time will be limited to 110 seconds. ASP.NET limits the execution of default threads to 110 seconds in .NET 2.0. http://msdn.microsoft.com/en-us/library/e1f13641(v=vs.80).aspx

Creating a queue — as suggested by others in earlier answers — is more reasonable and scalable.

http://msdn.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.executiontimeout.aspx

+1
source share

It is probably best to structure your method as follows:

 public void SendMails(DataTable dt) { foreach (DataRow row in dt.Rows) { // send emails } } 

And then name it like this:

 SendMails(dataTable); 

Or call the method using BackgroundWorker so that your user interface does not block.

0
source share

The first point is that if you explicitly create such threads, you are not using the thread pool. The CLR is committed to creating all of these threads, although ultimately it will create too much and drag out. 1000 explicit threads are too many.

Are you trying to do this on another thread because you want this to happen asynchronously or because you really want multiple threads to send?

If the first, try something like:

 ThreadStart ts1 = new ThreadStart(sendMails); Thread thread1 = new Thread(ts1); thread1.Start(); public void sendMails() { foreach (DataRow dataRow in dataTable.Rows) { //mail code } } 

If you feel that sending performance will be improved with multithreading, you need to manually reduce the number of threads created at any time, or use the .Net thread pool, since this will allow you to queue that will be blocked until the thread becomes free . This is certainly preferable to creating downloads of explicit threads.

0
source share

All Articles