简体   繁体   中英

Static dictionary contents not changing

I have a problem that I'm hoping is easily solved. I've scoured for an answer for the past few days, but no luck yet. What I'm trying to do is have a static dictionary represent connected clients, end goal being to send all of the connected clients data such as other player locations. Anyway, what I've done so far is when the client first connects, I add them to the static dictionary like so:

RegionServer.RegionClients.Add(UserId, this);
Log.DebugFormat("Added peer to region list");

foreach (var client in RegionServer.RegionClients)
{
     Log.DebugFormat("Client id in list : {0}", client.Key);
}

I added in the debug statements to make sure that the client is really being added to the list, of which it is.

Then in my main file I have:

public override  void InitBackgroundThreads()
    {
        ThreadPool.QueueUserWorkItem(SendUpdatesToAll);
    }

public void SendUpdatesToAll(object threadContext)
    {
        Stopwatch timer = new Stopwatch();
        timer.Start();
        isRunning = true;

        while (isRunning)
        {
            if (timer.Elapsed < TimeSpan.FromMilliseconds(100))
                continue;

            Update(timer.Elapsed);
            timer.Restart();
        }
    }

public void Update(TimeSpan elapsed)
    {

        foreach (KeyValuePair<Guid, Unity3dPeer> peer in RegionClients)
                {
                    Log.DebugFormat("Inside the foreach statement");
                }

In my debug log I can see that the loop is indeed looping, but the RegionClients dictionary values are never updated. The foreach loop is never iterated as there doesn't seem to be a client in the dictionary. Any help will be greatly appreciated, this problem has been driving me crazy! Thanks in advance.

-Jarryd

It turns out it was something simple. I have a cluster server going and I was trying to reference this dictionary from different processes...duh. Thanks for the ideas, it turns out was simpler than trying to be thread safe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM