Click here to Skip to main content
15,895,283 members
Home / Discussions / C#
   

C#

 
QuestionRotate Text OpenGL Pin
Paul Harsent27-Apr-10 0:29
Paul Harsent27-Apr-10 0:29 
AnswerRe: Rotate Text OpenGL [modified] Pin
Natural_Demon27-Apr-10 4:29
Natural_Demon27-Apr-10 4:29 
GeneralRe: Rotate Text OpenGL Pin
Paul Harsent27-Apr-10 5:30
Paul Harsent27-Apr-10 5:30 
GeneralRe: Rotate Text OpenGL Pin
Natural_Demon27-Apr-10 6:02
Natural_Demon27-Apr-10 6:02 
GeneralRe: Rotate Text OpenGL - OT Pin
DaveyM6927-Apr-10 6:16
professionalDaveyM6927-Apr-10 6:16 
GeneralRe: Rotate Text OpenGL - OT Pin
Natural_Demon27-Apr-10 6:44
Natural_Demon27-Apr-10 6:44 
GeneralRe: Rotate Text OpenGL - OT Pin
DaveyM6927-Apr-10 8:59
professionalDaveyM6927-Apr-10 8:59 
QuestionConcurrent access to private data [modified] Pin
Mystery12326-Apr-10 23:08
professionalMystery12326-Apr-10 23:08 
Hi,

maybe it's a simple problem for most of you, but I can't find an easy way to quite simple problem, let me describe:

Let's have this code:

<code>public ClassToChange()
{
	... //some other methods

	internal List<AnyDataClass> LockAndGetData()
	{
		Monitor.Enter(cs);
		return data;
	}

	internal Unlock()
	{
		Monitor.Exit(cs);
	}

	private object cs = new object();
	private List<AnyDataClass> data = new List<AnyDataClass>();
}

public class Editor
{
	public Editor(ClassToChange editedObject)
	{
		this.editedObject = editedObject;
	}

	public void MyFunctionToChangeData()
	{
		List<AnyDataClass> data = editedObject.LockAndGetData();
		
		... // change data

		editedObject.Unlock();
	}

	private ClassToChange editedObject;
}


class Program
{
	static void Main()
	{
		Editor ed = new Editor(new ClassToChange());
		ed.MyFunctionToChangeData();
	}
}</code>


So my aim is to let Editor change the data of ClassToChange in a thread-safe way. But as you can see to call all the List<anydataclass> data = editedObject.LockAndGetData(), then not to forget the editedObject.Unlock() is very boring to write (and not so safe). Do you have any easy solution for this?

Only solution I have, there might be the CS as attribute of ClassToChange and use lock {} in Editor, but I would need an attribute to every data object of ClassToChange, who would check if the cs is locked to make it safe. So that's also not a good way.

Any ideas?
Michal
modified on Tuesday, April 27, 2010 5:16 AM

AnswerRe: Concurrent access to private data Pin
Simon P Stevens27-Apr-10 0:10
Simon P Stevens27-Apr-10 0:10 
GeneralRe: Concurrent access to private data Pin
Mystery12327-Apr-10 6:32
professionalMystery12327-Apr-10 6:32 
QuestionString operation Pin
SRKSHOME26-Apr-10 22:47
SRKSHOME26-Apr-10 22:47 
AnswerRe: String operation Pin
Eddy Vluggen26-Apr-10 22:59
professionalEddy Vluggen26-Apr-10 22:59 
GeneralRe: String operation Pin
SRKSHOME26-Apr-10 23:06
SRKSHOME26-Apr-10 23:06 
GeneralRe: String operation Pin
Eddy Vluggen26-Apr-10 23:16
professionalEddy Vluggen26-Apr-10 23:16 
GeneralRe: String operation Pin
hossein narimani rad27-Apr-10 6:45
hossein narimani rad27-Apr-10 6:45 
AnswerRe: String operation Pin
Rajesh Anuhya26-Apr-10 23:07
professionalRajesh Anuhya26-Apr-10 23:07 
GeneralRe: String operation Pin
SRKSHOME26-Apr-10 23:15
SRKSHOME26-Apr-10 23:15 
GeneralRe: String operation Pin
Md. Marufuzzaman26-Apr-10 23:50
professionalMd. Marufuzzaman26-Apr-10 23:50 
GeneralRe: String operation Pin
PIEBALDconsult27-Apr-10 3:18
mvePIEBALDconsult27-Apr-10 3:18 
GeneralRe: String operation PinPopular
Luc Pattyn27-Apr-10 2:23
sitebuilderLuc Pattyn27-Apr-10 2:23 
AnswerRe: String operation Pin
PIEBALDconsult27-Apr-10 3:17
mvePIEBALDconsult27-Apr-10 3:17 
AnswerRe: String operation Pin
Ravi Bhavnani27-Apr-10 13:02
professionalRavi Bhavnani27-Apr-10 13:02 
QuestionHow can I get BITMAPINFOHEADER from Image Pin
TimSWatson26-Apr-10 22:22
TimSWatson26-Apr-10 22:22 
AnswerRe: How can I get BITMAPINFOHEADER from Image Pin
Rajesh Anuhya26-Apr-10 22:40
professionalRajesh Anuhya26-Apr-10 22:40 
GeneralRe: How can I get BITMAPINFOHEADER from Image Pin
TimSWatson26-Apr-10 23:18
TimSWatson26-Apr-10 23:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.