线程安全属性在C# [英] Thread Safe Properties in C#

查看:256
本文介绍了线程安全属性在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建在C#中的线程安全性,我想确保我在正确的路径 - 这里是我做了什么 -

I am trying to create thread safe properties in C# and I want to make sure that I am on the correct path - here is what I have done -

private readonly object AvgBuyPriceLocker = new object();
private double _AvgBuyPrice;
private double AvgBuyPrice 
{
    get
    {
        lock (AvgBuyPriceLocker)
        {
            return _AvgBuyPrice;
        }
    }
    set
    {
        lock (AvgBuyPriceLocker)
        {
            _AvgBuyPrice = value;
        }
    }
}

阅读此公告,似乎,好像这是不是这样做的正确方法 -

Reading this posting, it would seem as if this isn't the correct way of doing it -

C#线程安全具有get / set

不过,这篇文章似乎在暗示,否则,

however, this article seems to suggest otherwise,

<一个href="http://www.$c$cproject.com/KB/cs/Synchronized.aspx">http://www.$c$cproject.com/KB/cs/Synchronized.aspx

没有任何人有一个更明确的答案?

Does anybody have a more definitive answer?

编辑:

这是我想要做的getter / setter此属性的原因是B / C其实我希望它火当它设置一个事件 - 所以code实际上是这样的 -

The reason that I want to do the Getter/Setter for this property is b/c I actually want it to fire an event when it is set - so the code would actually be like this -

public class PLTracker
{

    public PLEvents Events;

    private readonly object AvgBuyPriceLocker = new object();
    private double _AvgBuyPrice;
    private double AvgBuyPrice 
    {
        get
        {
            lock (AvgBuyPriceLocker)
            {
                return _AvgBuyPrice;
            }
        }
        set
        {
            lock (AvgBuyPriceLocker)
            {
                Events.AvgBuyPriceUpdate(value);
                _AvgBuyPrice = value;
            }
        }
    }
}

public class PLEvents
{
    public delegate void PLUpdateHandler(double Update);
    public event PLUpdateHandler AvgBuyPriceUpdateListener;

    public void AvgBuyPriceUpdate(double AvgBuyPrice)
    {
        lock (this)
        {
            try
            {
                if (AvgBuyPriceUpdateListener!= null)
                {
                    AvgBuyPriceUpdateListener(AvgBuyPrice);
                }
                else
                {
                    throw new Exception("AvgBuyPriceUpdateListener is null");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

我是pretty的新来让我的code线程安全的,所以请随时告诉我,如果我在完全错误的方式去一下吧!

I am pretty new to making my code thread safe so please feel free to tell me if I am going about it in the totally wrong way!

威尔

推荐答案

既然你有一个原始值这个锁将正常工作 - 这个问题的另一个问题是,属性值是一个较为复杂的类(一个可变引用类型) - 锁定将保护访问和检索你的类持有的双重价值的实例

Since you have a primitive value this locking will work fine - the issue in the other question was that the property value was a more complex class (a mutable reference type) - the locking will protect accessing and retrieving the instance of the double value held by your class.

如果你的属性值,另一方面锁定一个可变的引用类型不会更改使用它的方法,一旦检索到的类的实例,这是什么其他的海报希望它做的保护。

If your property value is a mutable reference type on the other hand locking will not protect from changing the class instance once retrieved using its methods, which is what the other poster wanted it to do.

这篇关于线程安全属性在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆