Reputation: 3952
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):
Upvotes: 0
Views: 735
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 Task
s, which means it will run on the ThreadPool
. It also tries to use Task
s efficiently, so if you have a collection of with a million of items, it won't create a million Task
s.
Upvotes: 2
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
Reputation: 812
The .NET Framework provides four timers. Two of these are general-purpose multithreaded timers:
The other two are special-purpose single-threaded timers:
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