SPRunningJobCollection.GetInstance method

Gets an instance of a SPRunningJob object, or throws an exception if that object does not exist.

Namespace:  Microsoft.SharePoint.Administration
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
Public Function GetInstance ( _
    jobDefinitionId As Guid, _
    serverName As String _
) As SPRunningJob
'Usage
Dim instance As SPRunningJobCollection
Dim jobDefinitionId As Guid
Dim serverName As String
Dim returnValue As SPRunningJob

returnValue = instance.GetInstance(jobDefinitionId, _
    serverName)
public SPRunningJob GetInstance(
    Guid jobDefinitionId,
    string serverName
)

Parameters

Return value

Type: Microsoft.SharePoint.Administration.SPRunningJob
A SPRunningJob object that represents this instance.

Exceptions

Exception Condition
ArgumentOutOfRangeException

Invalid parameter, or job has just been deleted.

Remarks

There may be multiple instances on several servers, or the job could be limited to one running instance on the farm at a time.

Examples

The following code example iterates through the services on a SharePoint Foundation farm to display information about each job that is running for each service, or has not yet been deleted from the SPRunningJobCollection associated with each service. For SPRunningJob objects, all programmatic interaction is with properties; there are methods available in the SPRunningJobCollection object that allow the implementer to interact with a specific instance of a SPRunningJob object.

[C#]

SPFarm farm = SPFarm.Local.Farm;
SPServiceCollection services = SPFarm.Local.Services;
int rand_job;
Guid jobdefid;
SPRunningJob rj;
string servername;
string svc_name = string.Empty;
 foreach (SPService service in services) {
  SPRunningJobCollection runningJobs = service.RunningJobs;
  if (runningJobs.Count > 0) {
    if (svc_name == string.Empty) {
       svc_name = service.Name;
    }
    Console.WriteLine("****Job Collection Count is     " + runningJobs.Count);
    Console.WriteLine("****Job Collection Service is   " + runningJobs.Service);
    // For the GetInstance method, save off job information at random
    rand_job = runningJobs.Count / 2;
    Console.WriteLine("****Collection member " + rand_job + " is " + runningJobs[rand_job].JobDefinition);
    jobdefid = runningJobs[rand_job].JobDefinitionId;
    servername = runningJobs[rand_job].ServerName;
  }
  else {
    jobdefid = Guid.Empty;
    servername = null;
  }
  foreach (SPRunningJob runningJob in runningJobs) {
    Console.WriteLine("****Job Id is           " + runningJob.JobDefinitionId);
  }
  if (jobdefid != Guid.Empty) {
    // random jobdef from collection 
    // getinstance method
    rj = runningJobs.GetInstance(jobdefid, servername);
    Console.WriteLine("*@@* Job Definition is   " + rj.JobDefinition);
    Console.WriteLine("*@@* Job Id is           " + rj.JobDefinitionId);
    Console.WriteLine("*@@* Job Title is        " + rj.JobDefinitionTitle);
  }
}

See also

Reference

SPRunningJobCollection class

SPRunningJobCollection members

Microsoft.SharePoint.Administration namespace

SPFarm