Sinan AKYAZICI
Sinan AKYAZICI

Reputation: 3952

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?

Upvotes: 0

Views: 735

Answers (3)

svick
svick

Reputation: 245076

If you want to process a collection of items efficiently in parallel, my first choice would be something that was made exactly for that purpose: Parallel.ForEach().

Internally, it uses Tasks, which means it will run on the ThreadPool. It also tries to use Tasks efficiently, so if you have a collection of with a million of items, it won't create a million Tasks.

Upvotes: 2

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131774

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.

Upvotes: 3

s_nair
s_nair

Reputation: 812

The .NET Framework provides four timers. Two of these are general-purpose multithreaded timers:

  • System.Threading.Timer
  • System.Timers.Timer

The other two are special-purpose single-threaded timers:

  • System.Windows.Forms.Timer (Windows Forms timer)
  • System.Windows.Threading.DispatcherTimer (WPF timer)

So if you want your method to be executed in multi-threaded way/parallel then you have to go for the one among first two.

Find more about this along with sample source code here

Upvotes: -1

Related Questions