4

我一直在尝试按照以下方式进行工作,这是一个完整的代码示例,可以在 Visual Studio 中运行,它演示了我正在谈论的场景。这里的一切都按预期工作,当我更改播放器的 IsReady 值时,附加到事件的 lambda 被触发并且 if (to) 评估为 true。但是 Console.WriteLine 永远不会被命中,因为 ConcurrentBag 中播放器的 IsReady 值似乎没有更新。

class Program
{
    public static ConcurrentBag<Player> Players { get; set; }
    static void Main(string[] args)
    {
        Players = new ConcurrentBag<Player>();
        Player player = new Player() { Id = "123" };
        Players.Add(player);
        player.IsReady.ValueChanged += (from, to) =>
        {
            if (to)
            {
                if (Players.All(p => p.IsReady.Value))
                {
                    Console.WriteLine("It worked");
                }
            }
        };

        LookupPlayerById("123").IsReady.Value = true;
    }

    public static Player LookupPlayerById(string clientId)
    {
        var player = Players.FirstOrDefault(x => x.Id == clientId);
        return player;
    }
}

public class Player
{
    public string Id { get; set; }
    public MonitoredValue<bool> IsReady { get; set; }

    public Player()
    {
        IsReady = new MonitoredValue<bool>(false);
    }
}

public class MonitoredValue<T>
{
    public delegate void ValueChangedHandler(T from, T to);
    public event ValueChangedHandler ValueChanged;

    private T m_Value;
    public T Value
    {
        get { return m_Value; }
        set
        {
            if (ValueChanged != null) // if invocation list is not empty, fire the event
            {
                ValueChanged(m_Value, value);
            }
            m_Value = value;
        }
    }

    public MonitoredValue() { }

    public MonitoredValue(T initialValue)
    {
        m_Value = initialValue;
    }
}
4

2 回答 2

3

问题是您首先通知您的听众,然后实际更改该值。将您的属性定义更改为下面给出的代码:

delegate {}如果默认事件实现,现在我不需要在每次调用之前检查 null)

public event ValueChangedHandler ValueChanged = delegate {};

public T Value
{
    get { return m_Value; }
    set
    {
        //first change
        m_Value = value;

        //now notify
        ValueChanged(m_Value, value);

    }
}
于 2013-03-31T20:59:06.757 回答
0

由于它是一个引用类型,Player 将被 FirstOrDefault 返回的对象引用,除非它是一个默认值,在这种情况下你没有得到匹配。

演示示例:

    public static void Main()
    {
        ConcurrentBag<ClassA> test = new ConcurrentBag<ClassA>();
        var hurp = new ClassA();
        hurp.number = 3;
        test.Add(hurp);

        var derp = test.FirstOrDefault();
        derp.number = 4;

        Console.Write(test.FirstOrDefault().number);
        Console.WriteLine(derp.number);
        Console.ReadLine();
    }

印刷: 44

于 2013-03-31T20:58:59.090 回答