Your approach is good--I can't think of a better way.
The ArcanaDevelopment.adTempus.Shared.JobStatusHelpers static class has lists of JobState values for various categories (e.g., all JobState values that indicate an active instance). (Unfortunately there's no detailed documentation for this class at the moment; hopefully we can get that updated for you shortly.)
You want to look for anything that has a status that's included in ActiveStatuses or WaitingStatuses. Write your code to get the lists from JobStatusHelpers rather than hard-coding a list of values; that way you won't need to change your code if we add or reclassify values.
ActiveStatuses includes:
- JobState.Starting
- JobState.Running
- JobState.Killing
- JobState.Aborting
- JobState.WaitingForCondition
- JobState.WaitingForResource
- JobState.Queued
- JobState.AgentQueued
- JobState.AgentDispatched
WaitingStatuses includes:
- JobState.WaitingForCondition
- JobState.WaitingForResource
- JobState.WaitingForPreviousInstance
- JobState.WaitingForRestart
- JobState.Queued
- JobState.AgentQueued
You can use DataContext.GetJobHistory to fetch the instances, by setting the InstanceQueryParameters.Statuses to the list of statuses you want to query for (i.e., the combination of ActiveStatuses and WaitingStatuses).
To query within a particular group, add that group's OID to InstanceQueryParameters.TargetObjects. To query for all groups, use ArcanaDevelopment.adTempus.Shared.WellKnownOIDs.RootGroup.
Once you have fetched the instances, you can use ExecutionHistoryItem.IsRunning and IsWaiting to distinguish states easily. JobStatusHelpers also has a set of methods corresponding to the ActiveStatuses, WaitingStatuses, etc.
If you want to stop those "waiting" instances from running, you can use ExecutionHistoryItem.Terminate, which will cancel their wait so they don't execute.
Note that GetJobHistory only returns instances for jobs that you have permission for, so make sure your API client is connecting under a login that has permission on all jobs; otherwise there could be active jobs that you're not seeing.