views:

18

answers:

3

I have a list on which I have an ItemUpdated handler.

When I edit using the datasheet view and modify every item, the ItemUpdated event will obviously run for every single item.

In my ItemUpdated event, I want it to check if there is a Timer Job scheduled to run. If there is, then extend the SPOneTimeSchedule schedule of this job to delay it by 5 seconds. If there isn't, then create the Timer Job and schedule it for 5 seconds from now.

I've tried looking to see if the job definition exists in the handler and if it does exist, then extend the schedule by 5 seconds. If it doesn't exist, then create the job definition to run in a minutes time.

MyTimerJob rollupJob = null;
foreach (SPJobDefinition job in web.Site.WebApplication.JobDefinitions) 
{
    if (job.Name == Constants.JOB_ROLLUP_NAME)
    {
        rollupJob = (MyTimerJob)job;
    }
}
if (rollupJob == null)
{
        rollupJob = new MyTimerJob(Constants.JOB_ROLLUP_NAME, web.Site.WebApplication);
}

SPOneTimeSchedule schedule = new SPOneTimeSchedule(DateTime.Now.AddSeconds(5));
rollupJob.Schedule = schedule;
rollupJob.Update();

When I try this out on the server, I get a lot of errors

"An update conflict has occurred, and you must re-try this action. The object MyTimerJob Name=MyTimerJobName Parent=SPWebApplication Name=SharePoint -80 is being updated by NT AUTHORITY\NETWORK SERVICE in the w3wp process

I think the job is probably running for the first time and once running, the other ItemUpdated events are coming in and finding the existing Job definition. It then tries to Update this definition even though it is currently being used. Should I make a new Job Definition name so that it doesn't step on top of the first? Or raise the time to a minute?

A: 

This is because the event is asynchronous. You'll need to rethink exactly what you're trying to solve with this code and potentially re-factor it.

zincorp
Yep, I'm aware of the Updated and Added events being asynchronous - I need to use these events however as I'm only interested in the changes once they're made
Graeme
A: 

Maybe you should try using "lock" on the timer job object?

Vladi Gubler
A: 

I solved this myself by just setting the delay to a minutes time from now regardless of whether a definition is found. This way, while it is busy, it will keep pushing back the scheduling of the job until it is done processing

Graeme