/// <summary> /// Add job to the job queue. The method sends request to the mailbox. Thread wakes up, /// pulls the request from the mailbox queue, serve the request /// </summary> /// <param name="job"> /// A <see cref="Job"/> /// This method is the job required to be executed. /// Method is called from the thread. /// </param> /// <param name="jobDone"> /// A <see cref="JobDone"/> /// The method will be called after the job is comleted /// </param> /// <param name="jobArgument"> /// A <see cref="System.Object"/> /// Arbitrary data specified by the application. Can be null /// Methods "job" and "jobDone" are free to modify the state of the /// object. /// </param> /// <returns> /// A <see cref="System.Boolean"/> /// returns true is there was a place in the job queue and the job is queued /// </returns> public bool AddJob(Job job, JobDone jobDone, object jobArgument) { JobQueueParams jobParams = new JobQueueParams(job, jobDone, jobArgument); bool result = Send(jobParams); return(result); }
public void Update() { this.WorkHoursRequired -= Employee.WorkHoursPerWeek; if (this.WorkHoursRequired <= 0 && !this.IsDone) { JobDone?.Invoke(this, new JobEventArgs(this)); } }
public Task ExecuteJob(BaseJob baseJob) //async { ProcessJob = baseJob; StopWatch.Start(); Progress <string> progress = new Progress <string>(print); Task task = Task.Run(() => { //await baseJob.Execute(progress, CancellationToken.None); }); task.Wait(); StopWatch.Stop(); ProcessJob.ExecutionTime = StopWatch.Elapsed.Ticks; JobDone?.Invoke(this, ProcessJob); ProcessJob = null; return(task); }
/// <summary> /// Executes job in a thread, calls jobDone after the job done /// </summary> /// <param name="job"> /// A <see cref="Job"/> /// </param> /// <param name="jobDone"> /// A <see cref="JobDone"/> /// The method call when the job is done. Can be null /// </param> /// <param name="jobArgument"> /// A <see cref="System.Object"/> /// Arbitrary data specified by the application. Can be null /// </param> /// <returns> /// A <see cref="System.Boolean"/> /// </returns> public bool PlaceJob(Job job, JobDone jobDone, object jobArgument) { bool result = false; JobParams jobParams = default(JobParams); bool shouldSpawnJob; do { // allocate job params blocks lock (freeJobs) { if (freeJobs.Count > 0) { jobParams = freeJobs.Pop(); jobParams.Init(job, jobDone, jobArgument); pendingJobs.Enqueue(jobParams); // just to be sure that there is a thread to serve the new // job allocate a free thread (if there is any) shouldSpawnJob = (countRunningThreads == 0); countMaxJobs = Math.Max(countMaxJobs, pendingJobs.Count); countPlacedJobs++; result = true; } else // no room for a new job - get out { System.Console.WriteLine("Failed to place a job"); break; } } if (shouldSpawnJob) { RefreshQueue(); } result = true; }while (false); return(result); }
private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { time.Stop(); JobResult result = null; if (!e.Cancelled && e.Error == null) { currentJob.Finish(); result = (JobResult)e.Result; } else { currentJob.Cancel(); } JobDoneEventArg jobArg = new JobDoneEventArg(currentJob.Id, currentJob.Type, currentJob.State, result, time.ElapsedMilliseconds); JobDone?.Invoke(sender, jobArg); time.Reset(); }
protected virtual void OnJobDone(JobDoneEventArgs <CachedLog> e) { JobDone?.Invoke(this, e); }
private void Awake() { mar = this; }
public void Init(Job job, JobDone jobDone, object jobArgument) { this.job = job; this.jobDone = jobDone; this.jobArgument = jobArgument; }
public JobQueueParams(Job job, JobDone jobDone, object jobArgument) { Init(job, jobDone, jobArgument); }
public void OnJobDone(string result) { JobDone?.Invoke(result, EventArgs.Empty); }
protected virtual void OnJobDone() => JobDone?.Invoke(this, EventArgs.Empty);