C # Synchronized Object - Duplicate Code Using Writing Accessories

I like to use objects that are themselves synchronized using a private object that is locked when the property changes. Is there a general way to achieve this? My code always looks like this for each property (with one locker object)

private Object locker = new Object();
private int x; 
    public int X
    {
    get {return x;} 
    set{
        lock(locker){x=value;}
    }

Are you an easier way to achieve thread safe modification of properties.

+3
source share
3 answers

I keep wondering what C # can do with the new features of C # 3.0. I was about to throw something there that I thought would be so, but it turned out to be better than I had hoped. Here.

( " "... - ). , #. , . "SynchronicityHandler" (sucky term, ).

SynchronicityHandler . . , :

public class PersonValueHolder
{
     public string FirstName { get; set; }
     public string LastName { get; set; }
     public bool HasCollegeDegree { get; set; }
}

public class SyncronicityHandler<T>
{
     private object locker = new object();

     private T valueHolder;

     public SynchronicityHandler(T theValueHolder)
     {
          this.valueHolder = theValueHolder;
     }

     public void WorkWithValueHolderSafely(Action<T> yourAction)
     {
          lock(locker)
          {
               yourAction(valueHolder);
          }
     }
}

, :

var myPerson = new SynchronicityHandler(new PersonValueHolder());

// Safely setting values
myPerson.WorkWithValueHolderSafely( p => 
     {
          p.FirstName = "Douglas";
          p.LastName = "Adams";
          p.HasCollegeDegree = true;
     });

// Safely getting values (this syntax could be improved with a little effort)
string theFirstName = null;

myPerson.WorkWithValueHolderSafely( p=> theFirstName = p.FirstName);

Console.Writeline("Name is: " + theFirstName); // Outputs "Name is: Douglass".

, , , " " . .

+2

- . .

, . - , IMO.

, , . , , , . , , , , . , , .

- , , , , , - .

+8

Not to be an “enabler”, but if you decide that this is definitely the design you want and you find yourself repeating these properties again, you can save a few keystrokes and write a small piece of code to speed up the process of adding them.

    <?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>prop</Title>
            <Shortcut>propso</Shortcut>
            <Description>Locking property for SO</Description>
            <Author>TheMissingLinq</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID>
                    <ToolTip>Property type</ToolTip>
                    <Default>int</Default>
                </Literal>
                <Literal>
                    <ID>property</ID>
                    <ToolTip>Property name</ToolTip>
                    <Default>MyProperty</Default>
                </Literal>
                <Literal>
                    <ID>field</ID>
                    <ToolTip>Field Name</ToolTip>
                    <Default>myField</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp">
            <![CDATA[private $type$ $field$;
                public $type$ $property$ 
                {
                    get
                    {
                        lock(locker) { return this.$field$; }
                    }
                    set
                    { 
                        lock(locker) { this.$field$ = value; } 
                    }
                }
            $end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
+3
source

Source: https://habr.com/ru/post/1705039/


All Articles