Click here to Skip to main content
15,923,852 members
Home / Discussions / C#
   

C#

 
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 
AnswerRe: Concurrent access to private data Pin
Simon P Stevens27-Apr-10 0:10
Simon P Stevens27-Apr-10 0:10 
You could potentially use Action<T> delegates to do this. Something like this:
public class ClassToChange
{
    private object cs = new object();
    private List<String> data = new List<String>();

    public void LockAndMakeChange(Action<List<String>> changeAction)
    {
        lock (cs)
        {
            changeAction(data);
        }
    }
}

public class Editor
{
    private ClassToChange editedObject;

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

    public void MyFunctionToChangeData()
    {
        editedObject.LockAndMakeChange((data) =>
        {
            // change data. You are within the lock here.
            data.Add("new string");
        });
    }        
}


This would achieve what you are trying to do. It means your editor only has to call LockAndMakeChange and doesn't have to worry about unlocking afterwards because the class handles it after the action has completed, and your lock object remains private (which is good).

However, you need to be careful with this. By allowing external code to run inside an internally private lock you are introducing a great risk of deadlocking. It is having a similar effect to making your locks public (which is bad). If you aren't very careful other code may be able to take advantage of your exposed locking methods to crash or hang your app.

Instead, I would think about having the modification done entirely within the class so no external code runs within the lock. You could have methods like this that just took in the data and did the modification internally:
public void AddNewString(String newString)
{
    lock (cs)
    {
        data.Add(newString);
    }
}

public void AddNewString(params String[] newStrings)
{
    lock (cs)
    {
        data.AddRange(newStrings);
    }
}

public void AddNewString(IEnumerable<String> newStrings)
{
    lock (cs)
    {
        data.AddRange(newStrings);
    }
}
Then you would call it like this:
public void MyFunctionToChangeData()
{
    editedObject.AddNewString("new");

    editedObject.AddNewString("new1", "new2", "new3");

    List<String> newStrings = new List<string>();
    newStrings.Add("new4");
    newStrings.Add("new5");
    newStrings.Add("new6");
    editedObject.AddNewString(newStrings);
}


This keeps your encapsulation higher and reduces the risk of deadlocking.
Simon

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 
Questionstruct and class Pin
jitendra sandu26-Apr-10 21:07
jitendra sandu26-Apr-10 21:07 

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.