473,382 Members | 1,078 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

"Global" thread-safty in sharepoint / WS 3.0

Hi

We have some code that runs in a EventReceiver ItemAdded handler. The code
must be thread-safe since it is iterating a List to find the max number of a
column and assigns it + 1 to the Item that was added. If it was not
thread-safe, two items could get the same number (which must be unique).

So how do we garantee this in the event handler? Is locking on "this"
enough? I guess not since other types of Event Handlers could as well change
the number. So maybe we need a lock on the whole AppDomain. But how?

public override void ItemAdded(SPItemEventProperties properties)
{
lock(this)
{
// Do thread-safe code here
}
}

(Performance is no issue)

Regards
Anders
May 20 '07 #1
6 4228
Anders,

If the object is held as a field on your object, then locking on this
would work. However, you should have a separate object that is private that
you use specifically for locking. For example:

// The list.
private List<intmyList = ...;

// The lock on the list.
private readonly object myListLock = new object();

And you might want to encapsulate the operations on a list in separate
methods so that you don't miss locking the list at the appropriate time.

If the list is static, then the same thing applies, it's just that you
should make the field static (and not public, rather, expose your operations
through methods, and have the methods on the type access the private lock
object).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Anders J" <a@wrote in message
news:u9**************@TK2MSFTNGP02.phx.gbl...
Hi

We have some code that runs in a EventReceiver ItemAdded handler. The code
must be thread-safe since it is iterating a List to find the max number of
a column and assigns it + 1 to the Item that was added. If it was not
thread-safe, two items could get the same number (which must be unique).

So how do we garantee this in the event handler? Is locking on "this"
enough? I guess not since other types of Event Handlers could as well
change the number. So maybe we need a lock on the whole AppDomain. But
how?

public override void ItemAdded(SPItemEventProperties properties)
{
lock(this)
{
// Do thread-safe code here
}
}

(Performance is no issue)

Regards
Anders
May 20 '07 #2
Thank Nicholas,

Thanks for your suggestion. But i´m not convinced that i solves my problem.

Lets say i have this. Two users of a list in sharepoint do a action at the
exactly same time. The one Adds a teim an rasies the ItemAddedEventReceiver
and the other updates a item and rasises the ItemUpdatedEventReceiver. Both
these classes iterates through a Sharepoint list to find the highest number
available and increment that with 1 and assigns it to a column. This is true
both in the added an updated evenets.

The look up is done from two completly seperate objects from diffrent
clases. The lookup is in done in a database (Sharepoints through its API).

My concern is that locking on a local object in one of these event handlers
don´t affect the other. I some how need a AppPool wise "thing" to lock on so
that all objects of all classes will lock accordingly to this.

Example code below shows my concern. These locks can effect each other (i
think) but they MUST
--------------
public ItemAddedEventreceiver : SPEventReciver
{
private readonly object myLock = new object();
public override void ItemAdded(SPItemEventProperties properties)
{
lock(myLock)
{
// Get max key in list, incremt with 1 and assign to current item.
}
}
}

public ItemUpdatedEventreceiver : SPEventReciver
{
private readonly object myLock = new object();
public override void ItemUpdated(SPItemEventProperties properties)
{
lock(myLock)
{
// Get max key in list, incremt with 1 and assign to current item.
}
}
}

Regards
Anders

If the object is held as a field on your object, then locking on this
would work. However, you should have a separate object that is private
that you use specifically for locking. For example:

// The list.
private List<intmyList = ...;

// The lock on the list.
private readonly object myListLock = new object();

And you might want to encapsulate the operations on a list in separate
methods so that you don't miss locking the list at the appropriate time.

If the list is static, then the same thing applies, it's just that you
should make the field static (and not public, rather, expose your
operations through methods, and have the methods on the type access the
private lock object).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Anders J" <a@wrote in message
news:u9**************@TK2MSFTNGP02.phx.gbl...
>Hi

We have some code that runs in a EventReceiver ItemAdded handler. The
code must be thread-safe since it is iterating a List to find the max
number of a column and assigns it + 1 to the Item that was added. If it
was not thread-safe, two items could get the same number (which must be
unique).

So how do we garantee this in the event handler? Is locking on "this"
enough? I guess not since other types of Event Handlers could as well
change the number. So maybe we need a lock on the whole AppDomain. But
how?

public override void ItemAdded(SPItemEventProperties properties)
{
lock(this)
{
// Do thread-safe code here
}
}

(Performance is no issue)

Regards
Anders

May 20 '07 #3
Yes, which is why I suggested "static". If you are running in different
application domains (I don't know what you meant by app pool), then you are
going to have to use some sort of external resource to manage this (a
database, a file) and use the inherent characteristics of that store to
manage concurrency.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Anders J" <a@wrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
Thank Nicholas,

Thanks for your suggestion. But i´m not convinced that i solves my
problem.

Lets say i have this. Two users of a list in sharepoint do a action at the
exactly same time. The one Adds a teim an rasies the
ItemAddedEventReceiver and the other updates a item and rasises the
ItemUpdatedEventReceiver. Both these classes iterates through a Sharepoint
list to find the highest number available and increment that with 1 and
assigns it to a column. This is true both in the added an updated evenets.

The look up is done from two completly seperate objects from diffrent
clases. The lookup is in done in a database (Sharepoints through its API).

My concern is that locking on a local object in one of these event
handlers don´t affect the other. I some how need a AppPool wise "thing" to
lock on so that all objects of all classes will lock accordingly to this.

Example code below shows my concern. These locks can effect each other (i
think) but they MUST
--------------
public ItemAddedEventreceiver : SPEventReciver
{
private readonly object myLock = new object();
public override void ItemAdded(SPItemEventProperties properties)
{
lock(myLock)
{
// Get max key in list, incremt with 1 and assign to current item.
}
}
}

public ItemUpdatedEventreceiver : SPEventReciver
{
private readonly object myLock = new object();
public override void ItemUpdated(SPItemEventProperties properties)
{
lock(myLock)
{
// Get max key in list, incremt with 1 and assign to current item.
}
}
}

Regards
Anders

> If the object is held as a field on your object, then locking on this
would work. However, you should have a separate object that is private
that you use specifically for locking. For example:

// The list.
private List<intmyList = ...;

// The lock on the list.
private readonly object myListLock = new object();

And you might want to encapsulate the operations on a list in separate
methods so that you don't miss locking the list at the appropriate time.

If the list is static, then the same thing applies, it's just that you
should make the field static (and not public, rather, expose your
operations through methods, and have the methods on the type access the
private lock object).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Anders J" <a@wrote in message
news:u9**************@TK2MSFTNGP02.phx.gbl...
>>Hi

We have some code that runs in a EventReceiver ItemAdded handler. The
code must be thread-safe since it is iterating a List to find the max
number of a column and assigns it + 1 to the Item that was added. If it
was not thread-safe, two items could get the same number (which must be
unique).

So how do we garantee this in the event handler? Is locking on "this"
enough? I guess not since other types of Event Handlers could as well
change the number. So maybe we need a lock on the whole AppDomain. But
how?

public override void ItemAdded(SPItemEventProperties properties)
{
lock(this)
{
// Do thread-safe code here
}
}

(Performance is no issue)

Regards
Anders

May 20 '07 #4
again (to Anders J) .general is not a newsgroup for duplicates of posts that
are not about general sharepoint questions.

Follow-up amended accordingly.

--
Mike Walsh
WSS FAQ http://www.wssfaq.com
No private e-mail please.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:F7**********************************@microsof t.com...
Yes, which is why I suggested "static". If you are running in
different application domains (I don't know what you meant by app pool),
then you are going to have to use some sort of external resource to manage
this (a database, a file) and use the inherent characteristics of that
store to manage concurrency.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Anders J" <a@wrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
>Thank Nicholas,

Thanks for your suggestion. But i´m not convinced that i solves my
problem.

Lets say i have this. Two users of a list in sharepoint do a action at
the exactly same time. The one Adds a teim an rasies the
ItemAddedEventReceiver and the other updates a item and rasises the
ItemUpdatedEventReceiver. Both these classes iterates through a
Sharepoint list to find the highest number available and increment that
with 1 and assigns it to a column. This is true both in the added an
updated evenets.

The look up is done from two completly seperate objects from diffrent
clases. The lookup is in done in a database (Sharepoints through its
API).

My concern is that locking on a local object in one of these event
handlers don´t affect the other. I some how need a AppPool wise "thing"
to lock on so that all objects of all classes will lock accordingly to
this.

Example code below shows my concern. These locks can effect each other (i
think) but they MUST
--------------
public ItemAddedEventreceiver : SPEventReciver
{
private readonly object myLock = new object();
public override void ItemAdded(SPItemEventProperties properties)
{
lock(myLock)
{
// Get max key in list, incremt with 1 and assign to current item.
}
}
}

public ItemUpdatedEventreceiver : SPEventReciver
{
private readonly object myLock = new object();
public override void ItemUpdated(SPItemEventProperties properties)
{
lock(myLock)
{
// Get max key in list, incremt with 1 and assign to current item.
}
}
}

Regards
Anders

>> If the object is held as a field on your object, then locking on this
would work. However, you should have a separate object that is private
that you use specifically for locking. For example:

// The list.
private List<intmyList = ...;

// The lock on the list.
private readonly object myListLock = new object();

And you might want to encapsulate the operations on a list in
separate methods so that you don't miss locking the list at the
appropriate time.

If the list is static, then the same thing applies, it's just that
you should make the field static (and not public, rather, expose your
operations through methods, and have the methods on the type access the
private lock object).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Anders J" <a@wrote in message
news:u9**************@TK2MSFTNGP02.phx.gbl...
Hi

We have some code that runs in a EventReceiver ItemAdded handler. The
code must be thread-safe since it is iterating a List to find the max
number of a column and assigns it + 1 to the Item that was added. If it
was not thread-safe, two items could get the same number (which must be
unique).

So how do we garantee this in the event handler? Is locking on "this"
enough? I guess not since other types of Event Handlers could as well
change the number. So maybe we need a lock on the whole AppDomain. But
how?

public override void ItemAdded(SPItemEventProperties properties)
{
lock(this)
{
// Do thread-safe code here
}
}

(Performance is no issue)

Regards
Anders


May 20 '07 #5
(Sorry about that)

Now follow-ups *are* redirected away from sharepoint.general
Mike Walsh

"Mike Walsh" <en************@hotmail.comwrote in message
news:ue*************@TK2MSFTNGP02.phx.gbl...
again (to Anders J) .general is not a newsgroup for duplicates of posts
that are not about general sharepoint questions.

Follow-up amended accordingly.

--
Mike Walsh
WSS FAQ http://www.wssfaq.com
No private e-mail please.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:F7**********************************@microsof t.com...
> Yes, which is why I suggested "static". If you are running in
different application domains (I don't know what you meant by app pool),
then you are going to have to use some sort of external resource to
manage this (a database, a file) and use the inherent characteristics of
that store to manage concurrency.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Anders J" <a@wrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
>>Thank Nicholas,

Thanks for your suggestion. But i´m not convinced that i solves my
problem.

Lets say i have this. Two users of a list in sharepoint do a action at
the exactly same time. The one Adds a teim an rasies the
ItemAddedEventReceiver and the other updates a item and rasises the
ItemUpdatedEventReceiver. Both these classes iterates through a
Sharepoint list to find the highest number available and increment that
with 1 and assigns it to a column. This is true both in the added an
updated evenets.

The look up is done from two completly seperate objects from diffrent
clases. The lookup is in done in a database (Sharepoints through its
API).

My concern is that locking on a local object in one of these event
handlers don´t affect the other. I some how need a AppPool wise "thing"
to lock on so that all objects of all classes will lock accordingly to
this.

Example code below shows my concern. These locks can effect each other
(i think) but they MUST
--------------
public ItemAddedEventreceiver : SPEventReciver
{
private readonly object myLock = new object();
public override void ItemAdded(SPItemEventProperties properties)
{
lock(myLock)
{
// Get max key in list, incremt with 1 and assign to current item.
}
}
}

public ItemUpdatedEventreceiver : SPEventReciver
{
private readonly object myLock = new object();
public override void ItemUpdated(SPItemEventProperties properties)
{
lock(myLock)
{
// Get max key in list, incremt with 1 and assign to current item.
}
}
}

Regards
Anders
If the object is held as a field on your object, then locking on
this would work. However, you should have a separate object that is
private that you use specifically for locking. For example:

// The list.
private List<intmyList = ...;

// The lock on the list.
private readonly object myListLock = new object();

And you might want to encapsulate the operations on a list in
separate methods so that you don't miss locking the list at the
appropriate time.

If the list is static, then the same thing applies, it's just that
you should make the field static (and not public, rather, expose your
operations through methods, and have the methods on the type access the
private lock object).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Anders J" <a@wrote in message
news:u9**************@TK2MSFTNGP02.phx.gbl...
Hi
>
We have some code that runs in a EventReceiver ItemAdded handler. The
code must be thread-safe since it is iterating a List to find the max
number of a column and assigns it + 1 to the Item that was added. If
it was not thread-safe, two items could get the same number (which
must be unique).
>
So how do we garantee this in the event handler? Is locking on "this"
enough? I guess not since other types of Event Handlers could as well
change the number. So maybe we need a lock on the whole AppDomain. But
how?
>
public override void ItemAdded(SPItemEventProperties properties)
{
lock(this)
{
// Do thread-safe code here
}
}
>
(Performance is no issue)
>
Regards
Anders
>
May 20 '07 #6
>again (to Anders J) .general is not a newsgroup for duplicates of posts
>that are not about general sharepoint questions.
Follow-up amended accordingly.
Sorry about that. Will try post more appropriate next time.

Regards
Anders
May 20 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: mrbog | last post by:
I have an array/hash that stores path information for my app. As in, what directory this is in, what directory that's in, what the name of the site is, what the products are called, etc. It's...
7
by: Lyn | last post by:
Hi and Season's Greetings to all. I have a question regarding the use of a qualifier word "Global". I cannot find any reference to this in Access help, nor in books or on the Internet. "Global"...
9
by: Javaman59 | last post by:
I saw in a recent post the :: operator used to reach the global namespace, as in global::MyNamespace I hadn't seen this before, so looked it up in MSDN, which explained it nicely. My question...
2
by: Steve | last post by:
I am new to this newsgroup & to .NET in general. I have been playing around with Visual Studio .NET, building and rendering web pages using VB "code behind" files. My problem / question is; How...
3
by: Pierre | last post by:
Hello, In an aspx page (mypage.aspx) from a web projet, I would like to get the value of a variable of the projet that is declared as public in a module. The variable can be called from...
4
by: J Rice | last post by:
I have been experimenting with some thread programming, but as I'm doing this on my own I am worried I might be making a major mistake. Here's a brief rundown of what I am working on. Multiple...
5
by: dave | last post by:
If I have a class that hold, for instance, user settings that should be accessible to the entire program logic, what is a good paradigm to use? In C++, I would have made it a global object,...
10
by: Bub.Paulson | last post by:
A month ago I finally took the plunge and began learning C# and ASP.Net, coming from a Classic ASP and VBScript background. In my classic ASP, I had my own little library of code that I stuck in...
4
by: =?Utf-8?B?QWxleCBNdW5r?= | last post by:
My Web application is developed in C# Visual Studio 2005 Professional. After deploying the application to the production server I am getting the following error: <%@ Application...
4
ChrisWang
by: ChrisWang | last post by:
Hi, I am having trouble understanding the use of 'global' variables I want to use a global variable with the same name as a parameter of a function. But I don't know how to use them at the same...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.