Search code examples
.netmultithreadingbackgroundworkerthreadpooltask-parallel-library

Should I use threadpool,threads or backgroundworker with System.Timers.Timer?


public class JobRunner
{
    internal Timer Timer;
    internal readonly IEnumerable<YuartzJob> Jobs;

    public void Start()
    {
        this.Timer = new Timer(5000);
        this.Timer.Elapsed += Timer_Elapsed;
        this.Timer.Start();
    }

    public void Stop()
    {
        this.Timer.Stop();
    }

    internal void Timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        foreach (var job in Jobs)
        {
            MyActivator.RunJob(job);
        }
    }
}

public class MyActivator
{
    public static void RunJob(YuartzJob job)
    {
        var objectHandle = Activator.CreateInstance(job.AssemblyName, job.ClassName).Unwrap();
        var currentJob = (IJob)objectHandle;
        currentJob.Run();
    }
}

How should I do where I used MyActivator.RunJob(job):

  1. Should I call MyActivator.RunJob by using ThreadPool ?
  2. Should I call MyActivator.RunJob by using Threads ?
  3. Should I call MyActivator.RunJob by using BackgroundWorker ?
  4. Does it work this way without doing anything?

Solution

  • If you are targeting .NET 4 the easiest way is to use Tasks. Tasks execute a method using threads from the ThreadPool. The .NET runtime (actually, a TaskScheduler) makes sure the threadpool is not exhausted by too many tasks:

        foreach (var job in Jobs)
        {
            Task.Factory.StartNew(()=>MyActivator.RunJob(job));
        }
    

    Tasks also allow for easy cancellation, exception handling and chaining which is not possible when using the ThreadPool or your own threads.

    For earlier versions you should consider ThreadPool first and only create your own threads if the jobs are so many and long running that they risk exhausting the threadpool:

        foreach (var job in Jobs)
        {
            ThreadPool.QueueUserWorkItem(o=>MyActivator.RunJob(job));
        }
    

    Backgroundworker is considered obsolete in .NET 4+ and only suitable for very coarse-grained parallelism anyway. In your case you would have to create a separate worker for each job, subscribe to its events, run it and clean up after it.