473,609 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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(SPIte mEventPropertie s properties)
{
lock(this)
{
// Do thread-safe code here
}
}

(Performance is no issue)

Regards
Anders
May 20 '07 #1
6 4238
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.co m
"Anders J" <a@wrote in message
news:u9******** ******@TK2MSFTN GP02.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(SPIte mEventPropertie s 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 ItemAddedEventR eceiver
and the other updates a item and rasises the ItemUpdatedEven tReceiver. 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 ItemAddedEventr eceiver : SPEventReciver
{
private readonly object myLock = new object();
public override void ItemAdded(SPIte mEventPropertie s properties)
{
lock(myLock)
{
// Get max key in list, incremt with 1 and assign to current item.
}
}
}

public ItemUpdatedEven treceiver : SPEventReciver
{
private readonly object myLock = new object();
public override void ItemUpdated(SPI temEventPropert ies 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.co m
"Anders J" <a@wrote in message
news:u9******** ******@TK2MSFTN GP02.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(SPIte mEventPropertie s 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.co m

"Anders J" <a@wrote in message
news:%2******** *******@TK2MSFT NGP02.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
ItemAddedEventR eceiver and the other updates a item and rasises the
ItemUpdatedEven tReceiver. 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 ItemAddedEventr eceiver : SPEventReciver
{
private readonly object myLock = new object();
public override void ItemAdded(SPIte mEventPropertie s properties)
{
lock(myLock)
{
// Get max key in list, incremt with 1 and assign to current item.
}
}
}

public ItemUpdatedEven treceiver : SPEventReciver
{
private readonly object myLock = new object();
public override void ItemUpdated(SPI temEventPropert ies 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.co m
"Anders J" <a@wrote in message
news:u9******* *******@TK2MSFT NGP02.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(SPIte mEventPropertie s properties)
{
lock(this)
{
// Do thread-safe code here
}
}

(Performanc e 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.c omwrote in
message news:F7******** *************** ***********@mic rosoft.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.co m

"Anders J" <a@wrote in message
news:%2******** *******@TK2MSFT NGP02.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
ItemAddedEvent Receiver and the other updates a item and rasises the
ItemUpdatedEve ntReceiver. 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 ItemAddedEventr eceiver : SPEventReciver
{
private readonly object myLock = new object();
public override void ItemAdded(SPIte mEventPropertie s properties)
{
lock(myLock)
{
// Get max key in list, incremt with 1 and assign to current item.
}
}
}

public ItemUpdatedEven treceiver : SPEventReciver
{
private readonly object myLock = new object();
public override void ItemUpdated(SPI temEventPropert ies 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.co m
"Anders J" <a@wrote in message
news:u9****** ********@TK2MSF TNGP02.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(SPIte mEventPropertie s properties)
{
lock(this)
{
// Do thread-safe code here
}
}

(Performan ce is no issue)

Regards
Anders


May 20 '07 #5
(Sorry about that)

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

"Mike Walsh" <en************ @hotmail.comwro te in message
news:ue******** *****@TK2MSFTNG P02.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.c omwrote
in message news:F7******** *************** ***********@mic rosoft.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.co m

"Anders J" <a@wrote in message
news:%2******* ********@TK2MSF TNGP02.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
ItemAddedEven tReceiver and the other updates a item and rasises the
ItemUpdatedEv entReceiver. 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 ItemAddedEventr eceiver : SPEventReciver
{
private readonly object myLock = new object();
public override void ItemAdded(SPIte mEventPropertie s properties)
{
lock(myLock)
{
// Get max key in list, incremt with 1 and assign to current item.
}
}
}

public ItemUpdatedEven treceiver : SPEventReciver
{
private readonly object myLock = new object();
public override void ItemUpdated(SPI temEventPropert ies 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
appropriat e 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.co m
"Anders J" <a@wrote in message
news:u9***** *********@TK2MS FTNGP02.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(SPIte mEventPropertie s properties)
{
lock(this)
{
// Do thread-safe code here
}
}
>
(Performanc e 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
2693
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 called $glb. So, every function so far looks like this: function something() { global $glb; }
7
2681
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" seems to be recognised by Access in at least three cases:- 1) "Global Const". Recently someone in this group helped me resolve a problem, and it involved the use of a Global Const. By Googling "Global Const", I got plenty of hits -- but they...
9
2482
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 is, do "global" and "::" always go together? Is there any other use for these operators, than as a pair? TIA,
2
3698
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 do I ensure that changes made to the "Global.asax.vb" file are immediately reflected in the "Global.asax" file? After I change to the "Global.asax.vb" file, the "Global.asax" file date modified does not change and I do not see the updated values...
3
3933
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 anywhere in the code behind page and in the class files, but when I try to call it from an aspx page, it raises an error. <%=myvar%> in mypage.aspx raise an error : the name "myvar" is not declared.
4
2489
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 threads, via Queue, are used to perform RBL checks on an IP. The threads are passed a defined class (ConnectionScore) in their init. This is used to collect the results, including a method to add the result. If I run the program, everything...
5
4932
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, protected if necessary for thread safety. In C#, of course, there are no global objects. The Program object is static, so cannot contain object instances. While I could store it in my main form pass it around, that seems cumbersome. I could do a...
10
6929
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 an include file called "Common_VBscript.asp" which had all of the common stuff I used. (Constants, Connection Strings, little utlities like "email validation" functions, or functions to ready strings for db entry, etc.) What is the best way...
4
5921
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 Codebehind="Global.asax.cs" Inherits="WebApplication.Global" Language="C#" %> I found numerous references to this problem on the web and tried what seems like a few (tens) suggestions, at least the ones I was able to understand. But alas no luck. Pleeeeeeeease...
4
3405
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 time. Here is a snippet of example code: def foo (a): global p global a p = a + 3 #here "a" will be reference to the global one a = 1
0
8076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8541
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8222
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8406
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4021
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.