/// <returns>the thread state</returns> public virtual int GetState() { JobControl.ThreadState state = base.GetThreadState(); if (state == JobControl.ThreadState.Running) { return(0); } if (state == JobControl.ThreadState.Suspended) { return(1); } if (state == JobControl.ThreadState.Stopped) { return(2); } if (state == JobControl.ThreadState.Stopping) { return(3); } if (state == JobControl.ThreadState.Ready) { return(4); } return(-1); }
/// <summary>resume the suspended thread</summary> public virtual void Resume() { if (this.runnerState == JobControl.ThreadState.Suspended) { this.runnerState = JobControl.ThreadState.Running; } }
/// <summary>suspend the running thread</summary> public virtual void Suspend() { if (this.runnerState == JobControl.ThreadState.Running) { this.runnerState = JobControl.ThreadState.Suspended; } }
/// <summary>Construct a job control for a group of jobs.</summary> /// <param name="groupName">a name identifying this group</param> public JobControl(string groupName) { // The thread can be in one of the following state // the thread state this.nextJobID = -1; this.groupName = groupName; this.runnerState = JobControl.ThreadState.Ready; }
/// <summary>The main loop for the thread.</summary> /// <remarks> /// The main loop for the thread. /// The loop does the following: /// Check the states of the running jobs /// Update the states of waiting jobs /// Submit the jobs in ready state /// </remarks> public virtual void Run() { try { this.runnerState = JobControl.ThreadState.Running; while (true) { while (this.runnerState == JobControl.ThreadState.Suspended) { try { Sharpen.Thread.Sleep(5000); } catch (Exception) { } } //TODO the thread was interrupted, do something!!! lock (this) { IEnumerator <ControlledJob> it = jobsInProgress.GetEnumerator(); while (it.HasNext()) { ControlledJob j = it.Next(); Log.Debug("Checking state of job " + j); switch (j.CheckState()) { case ControlledJob.State.Success: { successfulJobs.AddItem(j); it.Remove(); break; } case ControlledJob.State.Failed: case ControlledJob.State.DependentFailed: { failedJobs.AddItem(j); it.Remove(); break; } case ControlledJob.State.Ready: { j.Submit(); break; } case ControlledJob.State.Running: case ControlledJob.State.Waiting: { //Do Nothing break; } } } } if (this.runnerState != JobControl.ThreadState.Running && this.runnerState != JobControl.ThreadState .Suspended) { break; } try { Sharpen.Thread.Sleep(5000); } catch (Exception) { } //TODO the thread was interrupted, do something!!! if (this.runnerState != JobControl.ThreadState.Running && this.runnerState != JobControl.ThreadState .Suspended) { break; } } } catch (Exception t) { Log.Error("Error while trying to run jobs.", t); //Mark all jobs as failed because we got something bad. FailAllJobs(t); } this.runnerState = JobControl.ThreadState.Stopped; }
/// <summary> /// set the thread state to STOPPING so that the /// thread will stop when it wakes up. /// </summary> public virtual void Stop() { this.runnerState = JobControl.ThreadState.Stopping; }