Search code examples
c#sharepointsharepoint-2016sharepoint-timer-job

How to set progress in custom long running timer job


I need some logical advice for setting progress in custom long running timer job (more than half an hour).

I have loop for more than 50k items in timer job Execute.

How to elegant update progress bar every minute or two using SPJobDefinition.UpdateProgress(int percentage) method?

I don't want update progress for each iteration in for-loop (too many progress updates).

sample code

 SPListItemCollection collListItems = list.GetItems();
    for (var i = 0; i < collListItems.Count; i++)
    { 
     //code
    }

Solution

  • Make 10 or 20 updates then. I mean something like that:

    var percentBlock = 10;
    var updateBlock = collListItems.Count / percentBlock;
    SPListItemCollection collListItems = list.GetItems();
    for (var i = 0; i < collListItems.Count; i++)
    { 
        if(i % updateBlock == 0)
            SPJobDefinition.UpdateProgress(i / updateBlock * percentBlock);
        //code
    }
    

    I think this would be more fair updates than periodical.