Thursday, August 30, 2007

Running event handlers

Hi!

I was presented with a problem while working at a customer of mine. When running event handlers, specially ItemUpdated, the code might be run several times by different threads. The thing is that the eventhandler makes some changes to the list item that generates an error if the code is run again. That is to say that the name of the item changes in the eventhandler. The problem consists of making sure that the code is only run once.

To solve this problem, I used a hashtable to store a temporary variable. Using a hashtable has the advantage that once the variable is set in the hastable, a try/catch loop will sort out the following threads since the hastable will generate an error if trying to insert duplicates.

Thus, when an error is received from the hashtable, a return statement is set, allowing only the first thread to run the code. Once the code is run, don't forget to reset the hashtable.

Since Gustaf, a colluege of mine, come up with the original idea, here is a link to his blog, also with code examples. http://gustafwesterlund.blogspot.com/2007/08/how-to-stop-infinite-recursionsloops-in.html

//Sebastian

Posted by Zeb at 14:24:47 | Permanent Link | Comments (0) |
Comments
Write a comment