Threadpool or TPL for a long running tasks RRS feed

  • Question

  • User-525837012 posted

    I have a windows service which sends out emails after a lengthy process. This service keep on fetching email data from DB table, whenever there is a table entry and process it and will send it out.

    Currently it is a multi thread application where we configure Thread count up to 25 in production server(which is solely for this purpose) as this is meant to run 24x7x365 . But we see only 2 active threads. What could be the reason?

    Also I wish to change the threading code here using thread pool or TPL. Could you please suggest me a better way to handle this scenario ?

    Thanks in Advance !

    //Sample code below

    Thread[] threads;
    int ThreadCount = 25;
    private void StartProcess()
    Create new threads
    if (Threads == null)
    // Create array of threads based on the configuration
    threads = new Thread[ThreadCount];
    for (int i = 0; i < ThreadCount; i++)
    Thread[] threads[i] = new Thread(new ThreadStart(SendEmail));
    resume it if exists
    for (int j = 0; j < threads.Length; j++)
    if (threads[j].ThreadState == Threading.ThreadState.Suspended)
    public void SendEmail()
    while (Thread.CurrentThread.ThreadState == System.Threading.ThreadState.Running)
    // send email code

    Tuesday, April 28, 2015 7:56 AM

All replies

  • User103196646 posted

    Hello Samk1984! Thanks for your post!

    Using the new Task Parallel Library (TPL) is a piece of cake. It's so much simpler and easier than the old way.

    You can store your thread count in the App.config file and do it this way:

    using System.Threading.Tasks;
    int threadCount;
    int.TryParse(ConfigurationManager.AppSettings["ThreadCount"], out threadCount);
    var po = new ParallelOptions { MaxDegreeOfParallelism = threadCount, TaskScheduler = null };
    // Get your list of emails here
    lstEmail = GetEmails();
    // Multi-thread - process each email
    Parallel.ForEach(lstEmail, po, plan =>
       // Everything in here will be multi-threaded using the TPL

    Also, just a quick architecture note - you could also set up a timer in your Windows Service and have the service check for new emails every n minutes - if any emails exist you could launch a separate multi-threaded .NET console app that processes the emails:

    using System.ServiceProcess;
    public class WinService : ServiceBase
            private readonly Timer _timer = new Timer();
            protected override void OnStart(string[] args)
                Logger.Info("Service Started");
                // Raise event
                _timer.Elapsed += timer_Elapsed;
                // 1000 is the number of milliseconds in a second
                // 60 is the number of seconds in a minute
                _timer.Interval = 1000 * 60;
                _timer.Enabled = true;
            // Handles event, every n minutes
            private static void timer_Elapsed(object sender, EventArgs e)
            private static void LaunchEmailEngine(string args)
                    var startInfo = new ProcessStartInfo
                        FileName = Helper.GetAppSetting("EngineLocation"),
                        Arguments = args


    Tuesday, April 28, 2015 1:14 PM