无法理解变量未更新的原因

时间:2016-08-08 19:08:30

标签: c# multithreading photon

我是一名新手程序员,我很难理解为什么这不起作用。 我有一个“ConnectedClientList”类:

public class ConnectedClientList
{
    static readonly object _lock = new object();
    public static IList<ClientPeer> ConnectedClientPeers;

    static ConnectedClientList()
    {
         ConnectedClientPeers = new List<ClientPeer>();
    }


    public static IList<ClientPeer> GetClientPeers()
    {
        lock (_lock)
        {
            return ConnectedClientPeers;
        }

    }

    public static void AddClientPeerToConnectedList(ClientPeer client)
    {
        lock (_lock)
        {
            ConnectedClientPeers.Add(client);
        }                
    }

    public void RemoveClientPeerFromConnectedList(ClientPeer client)
    {
        ConnectedClientPeers.Remove(client);
    }
}

我在另一堂课中有这个:

ClientPeer clientPeer = new ClientPeer(request, Logger, _clientHandlerRegistry);

ConnectedClientList.AddClientPeerToConnectedList(clientPeer);
Logger.InfoFormat("Added clientpeer to list. Count is now: {0}", ConnectedClientList.ConnectedClientPeers.Count);

最后我有了这个:

public CharacterUpdateEventHandler(ILogger logger)
    {
        Logger = logger;
        IFiber testFiber = new ThreadFiber();
        testFiber.Start();
        testFiber.ScheduleOnInterval(SendUpdateEventToClients, 1000, 5000);
    }

    public void SendUpdateEventToClients()
    {
        if(ConnectedClientList.GetClientPeers() != null)
            Logger.InfoFormat("# of connected client peers: {0}", ConnectedClientList.GetClientPeers().Count);
        else
        {
            Logger.InfoFormat("ConnectedClientPeers is null");
        }
    }

我正在尝试使用Exitgame的Photon Server构建服务器。当我第一次创建clientpeer然后将其添加到“ConnectedClientPeers”时,我立即得到调试信息,说“ConnectedClientPeers”列表实际上现在的计数为1.

我正在运行的线程似乎正在重复“SendUpdateEventToClients”,但该函数中的调试消息总是说我的“ConnectedClientPeers”列表的计数为0.

我确信这可能是我不知道的简单事情,但我没有任何运气找到过去几天的解决方案。非常感谢任何帮助或建议。谢谢!

-Jarryd

- 编辑 -

我更新了类以使整个类保持静态,但仍然存在同样的问题。

public static class ConnectedClientList
{
    static readonly object _lock = new object();
    public static readonly IList<ClientPeer> ConnectedClientPeers;

    static ConnectedClientList()
    {
         ConnectedClientPeers = new List<ClientPeer>();
    }


    public static IList<ClientPeer> GetClientPeers()
    {
        lock (_lock)
        {
            return ConnectedClientPeers;
        }

    }

    public static void AddClientPeerToConnectedList(ClientPeer client)
    {
        lock (_lock)
        {
            ConnectedClientPeers.Add(client);
        }                
    }

    public static void RemoveClientPeerFromConnectedList(ClientPeer client)
    {
        ConnectedClientPeers.Remove(client);
    }
}

我的调试日志:     2016-08-08 14:27:21,771 [ThreadFiber-1] INFO [CharacterUpdateEventHandler] [55] [(null)] - 连接的客户端对等数:0

2016-08-08 14:27:23,428 [ThreadFiber-2] INFO [CharacterUpdateEventHandler] [55] [(null)] - 连接的客户端对等数:0

2016-08-08 14:27:26,771 [ThreadFiber-1] INFO [CharacterUpdateEventHandler] [55] [(null)] - 连接的客户端对等数:0

2016-08-08 14:27:28,443 [ThreadFiber-2] INFO [CharacterUpdateEventHandler] [55] [(null)] - 连接的客户端对等数:0

2016-08-08 14:27:31,774 [ThreadFiber-1] INFO [CharacterUpdateEventHandler] [55] [(null)] - 连接的客户端对等数:0

2016-08-08 14:27:33,453 [ThreadFiber-2] INFO [CharacterUpdateEventHandler] [55] [(null)] - 连接的客户端对等数:0

2016-08-08 14:27:36,787 [ThreadFiber-1] INFO [CharacterUpdateEventHandler] [55] [(null)] - 连接的客户端对等数:0

2016-08-08 14:27:38,465 [ThreadFiber-2] INFO [CharacterUpdateEventHandler] [55] [(null)] - 连接的客户端对等数:0

1 个答案:

答案 0 :(得分:0)

不确定如何在评论中发布代码,这很多代码似乎都很好

  using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication6
    {
        class Program
        {
            static void Main(string[] args)
            {
                ClientPeer clientPeer = new ClientPeer();
                clientPeer.MyProperty = 5;
                ConnectedClientList.AddClientPeerToConnectedList(clientPeer);
                Console.WriteLine(ConnectedClientList.ConnectedClientPeers.Count.ToString());

                ConnectedClientList.AddClientPeerToConnectedList(clientPeer);
                Console.WriteLine(ConnectedClientList.ConnectedClientPeers.Count.ToString());


                clientPeer = new ClientPeer();
                ConnectedClientList.AddClientPeerToConnectedList(clientPeer);
                Console.WriteLine(ConnectedClientList.ConnectedClientPeers.Count.ToString());

            }

            public class ClientPeer
            {

                public int MyProperty { get; set; }

            }

            public static class ConnectedClientList
            {
                static readonly object _lock = new object();
                public static IList<ClientPeer> ConnectedClientPeers;

                static ConnectedClientList()
                {
                    ConnectedClientPeers = new List<ClientPeer>();
                }


                public static IList<ClientPeer> GetClientPeers()
                {
                    lock (_lock)
                    {
                        return ConnectedClientPeers;
                    }

                }

                public static void AddClientPeerToConnectedList(ClientPeer client)
                {
                    lock (_lock)
                    {
                        ConnectedClientPeers.Add(client);
                    }
                }


            }
        }
    }

所以这只添加了3个对等体并显示了计数并显示确实有3个