/// <summary>Stop all jobs currently running</summary> public void Stop() { lock (this) { if (server != null) { cancelToken.Cancel(); server.StopListening(); server = null; DeleteRunners(); runningJobs.Clear(); try { jobs.Completed(); } catch (Exception err) { errors += Environment.NewLine + err.ToString(); } if (AllJobsCompleted != null) { AllCompletedArgs args = new AllCompletedArgs(); if (errors != null) { args.exceptionThrown = new Exception(errors); } AllJobsCompleted.Invoke(this, args); } } } }
/// <summary>Called when main worker thread has exited.</summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="RunWorkerCompletedEventArgs"/> instance containing the event data.</param> private void OnWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (AllJobsCompleted != null) { AllJobsCompleted.Invoke(this, new EventArgs()); } }
/// <summary>Stop all jobs currently running</summary> public void Stop() { lock (this) { if (server != null) { cancelToken.Cancel(); server.StopListening(); server = null; DeleteRunners(); runningJobs.Clear(); jobs.Completed(); if (AllJobsCompleted != null) { AllCompletedArgs args = new AllCompletedArgs(); if (errors != null) { args.exceptionThrown = new Exception(errors); } AllJobsCompleted.Invoke(this, args); } } } }
/// <summary>Run the jobs synchronously, without extra threads.</summary> /// <remarks>Non threaded runs are useful for profiling.</remarks> public void Run() { foreach (Job job in jobs) { job.Run(this, null); } if (AllJobsCompleted != null) { AllJobsCompleted.Invoke(this, new EventArgs()); } }
/// <summary>Run the specified jobs</summary> /// <param name="jobs">An instance of a class that manages all jobs.</param> /// <param name="wait">Wait until all jobs finished before returning?</param> /// <param name="numberOfProcessors">The maximum number of cores to use.</param> public void Run(IJobManager jobs, bool wait = false, int numberOfProcessors = -1) { // No - get another job to run. IRunnable job = jobs.GetNextJobToRun(); CancellationTokenSource cancelToken = new CancellationTokenSource(); // Iterate through all jobs and run them. while (job != null) { JobCompleteArgs jobCompleteArguments = new JobCompleteArgs(); jobCompleteArguments.job = job; try { job.Run(cancelToken); } catch (Exception err) { jobCompleteArguments.exceptionThrowByJob = err; } if (JobCompleted != null) { JobCompleted.Invoke(this, jobCompleteArguments); } job = jobs.GetNextJobToRun(); } Exception exceptionThrown = null; try { jobs.Completed(); } catch (Exception err) { exceptionThrown = err; } if (AllJobsCompleted != null) { AllJobsCompleted.Invoke(this, new AllCompletedArgs() { exceptionThrown = exceptionThrown }); } }
/// <summary>Main DoWork method for the scheduler thread. NB this does NOT run on the UI thread. /// </summary> /// <param name="jobs">An instance of a class that manages all jobs.</param> private void JobRunnerThread(IJobManager jobs) { // No - get another job to run. IRunnable job = jobs.GetNextJobToRun(); Exception exceptionThrown = null; try { // Iterate through all jobs and run them. while (job != null && !cancelToken.IsCancellationRequested) { JobCompleteArgs jobCompleteArguments = new JobCompleteArgs(); jobCompleteArguments.job = job; try { job.Run(cancelToken); } catch (Exception err) { jobCompleteArguments.exceptionThrowByJob = err; } if (JobCompleted != null) { JobCompleted.Invoke(this, jobCompleteArguments); } job = jobs.GetNextJobToRun(); } jobs.Completed(); } catch (Exception err) { exceptionThrown = err; } allJobsFinished = true; AllJobsCompleted?.Invoke(this, new AllCompletedArgs() { exceptionThrown = exceptionThrown }); }
/// <summary>Main DoWork method for the scheduler thread. NB this does NOT run on the UI thread. /// </summary> /// <param name="jobs">An instance of a class that manages all jobs.</param> /// <param name="numberOfTasksToUse">The number of tasks to run asyhchronously</param> private void JobRunnerThread(IJobManager jobs, int numberOfTasksToUse = -1) { Exception exceptionThrown = null; try { numberTasksRunning = 0; // Main worker thread for keeping jobs running while (!cancelToken.IsCancellationRequested) { // Have we reached our maximum number of running jobs? if (numberTasksRunning >= numberOfTasksToUse) { Thread.Sleep(200); // Yes } else { // No - get another job to run. IRunnable job = jobs.GetNextJobToRun(); // If no job available then exit - we're done. if (job == null) { break; } // If the job is computationally intensive and will potentially take some time // to run, then we want to keep track of how many of these are running. if (typeof(IComputationalyTimeConsuming).IsAssignableFrom(job.GetType())) { lock (this) numberTasksRunning++; } // Run the job. Task.Run(() => { JobCompleteArgs jobCompleteArguments = new JobCompleteArgs(); jobCompleteArguments.job = job; try { job.Run(cancelToken); } catch (Exception err) { jobCompleteArguments.exceptionThrowByJob = err; } if (JobCompleted != null) { JobCompleted.Invoke(this, jobCompleteArguments); } lock (this) numberTasksRunning--; }); } } // All jobs now completed while (numberTasksRunning > 0) { Thread.Sleep(200); } jobs.Completed(); } catch (Exception err) { exceptionThrown = err; } if (AllJobsCompleted != null) { AllJobsCompleted.Invoke(this, new AllCompletedArgs() { exceptionThrown = exceptionThrown }); } }
/// <summary>Main DoWork method for the scheduler thread. NB this does NOT run on the UI thread. /// </summary> /// <param name="jobs">An instance of a class that manages all jobs.</param> /// <param name="numberOfTasksToUse">The number of tasks to run asyhchronously</param> private void JobRunnerThread(IJobManager jobs, int numberOfTasksToUse = -1) { Exception exceptionThrown = null; try { int numberTasksRunning = 0; // Main worker thread for keeping jobs running while (!cancelToken.IsCancellationRequested) { // Have we reached our maximum number of running jobs? if (numberTasksRunning >= numberOfTasksToUse) { Thread.Sleep(200); // Yes } else { // No - get another job to run. IRunnable job = jobs.GetNextJobToRun(); // If no job available then exit - we're done. if (job == null) { break; } lock (this) numberTasksRunning++; // Run the job. Task.Run(() => { JobCompleteArgs jobCompleteArguments = new JobCompleteArgs(); jobCompleteArguments.job = job; try { job.Run(cancelToken); } catch (Exception err) { jobCompleteArguments.exceptionThrowByJob = err; } if (JobCompleted != null) { JobCompleted.Invoke(this, jobCompleteArguments); } lock (this) numberTasksRunning--; }); } } // All jobs now completed while (numberTasksRunning > 0) { Thread.Sleep(200); } jobs.Completed(); } catch (Exception err) { exceptionThrown = err; } if (AllJobsCompleted != null) { AllJobsCompleted.Invoke(this, new AllCompletedArgs() { exceptionThrown = exceptionThrown }); } }