473,396 Members | 1,805 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,396 software developers and data experts.

class property locking

May I have anyone help?
I have a class with a property which could be accessed by different threads.

Can I put a lock block or a mutex in a get/set property to allow only a thread at time access?
The code I'm thinking sounds like this:

public class Dispatcher(){

private int p_State;

public State {

set{
int l_State;
lock(this){
l_State=p_State;
}
return l_State;
}
get{
lock(this){
p_State=value;
}
}

}

}

Thnx in advice to anyone helps me!
--------------------------------
From: Davide Villa

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>sWPhnCz+xUCjmmmSAVFXxA==</Id>
Jul 21 '05 #1
1 1032
Can I put a lock block or a mutex in a get/set property to allow only a thread at time access?


Yes, just like any other method.

But I don't recommend locking on the this reference. See
http://www.yoda.arachsys.com/csharp/...ckchoice.shtml

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #2

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

Similar topics

8
by: Matthew Bell | last post by:
Hi, I've got a question about whether there are any issues with directly calling attributes and/or methods of a threaded class instance. I wonder if someone could give me some advice on this. ...
1
by: john | last post by:
I have a service that checks the event log for certain types of error messages. It kept locking up so I put in debugging code to see where the problem was. It's locking up when I call the property...
3
by: MrFile | last post by:
C#.Net question for everyone I have a class that has a method that i run async from the Form. This class has one field string called log. I need this since i have an event that can write to it...
9
by: Jim Starcher | last post by:
I cannot get the intellisense hints to work on my class constructors. Would you please tell me what I am doing wrong? public class MyObject { //Constructors /// <summary> /// Used to...
1
by: Davide Villa via .NET 247 | last post by:
May I have anyone help? I have a class with a property which could be accessed by different threads. Can I put a lock block or a mutex in a get/set property to allow only a thread at time access?...
5
by: Scott Starker | last post by:
Is there anyway to do this? Every time any button is click inside class Form1, MyButtomArray.CharArray (MyButtomArray is a class) gets set (or reset) (bool). Once this is done the class TEC gets...
2
by: Vivek Ragunathan | last post by:
Hi Are the members in a static class in C# class synchronized for multiple thread access. If yes, are all static members in a C# class auto synchronized ? Regards Vivek Ragunathan
10
by: gerry | last post by:
Is there any way to specify that all methods of a class should be synchronized other than applying the attribute for every method ? Gerry
6
by: toton | last post by:
Hi, If I have a singleton class based on dynamic initialization (with new ) , is it considered a memory leak? Anything in C++ standard says about it ? And little off - topic question , If the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.