English 中文(简体)
问题,同时在列举时增加新价值
原标题:Problem while adding a new value to a hashtable when it is enumerated

......

I am doing a simple synchronous socket programming,in which i employed twothreads one for accepting the client and put the socket object into a collection,other thread will loop through the collection and send message to each client through the socket object.

问题是

1.i 连接客户与服务器,并开始发送电文

2.now i want to connect a new client,while doing this i cant update the collection and add a new client to my hashtable.it raises an exception "collection modified .Enumeration operation may not execute"

如何在不出现麻烦的情况下增加新的价值。

 private void Listen()
    {
        try
        {
            //lblStatus.Text = "Server Started Listening";
            while (true)
            {
                    Socket ReceiveSock = ServerSock.Accept();
                    //keys.Clear();
                    ConnectedClients = new ListViewItem();
                    ConnectedClients.Text = ReceiveSock.RemoteEndPoint.ToString();
                    ConnectedClients.SubItems.Add("Connected");
                    ConnectedList.Items.Add(ConnectedClients);
                    ClientTable.Add(ReceiveSock.RemoteEndPoint.ToString(), ReceiveSock);
                    //foreach (System.Collections.DictionaryEntry de in ClientTable)
                    //{

                    //    keys.Add(de.Key.ToString());
                    //}
                    //ClientTab.Add(
                    //keys.Add(

            }
            //lblStatus.Text = "Client Connected Successfully.";
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void btn_receive_Click(object sender, EventArgs e)
    {
        Thread receiveThread = new Thread(new ThreadStart(Receive));
        receiveThread.IsBackground = true;
        receiveThread.Start();
    }
    private void Receive()
    {
        while (true)
        {
            //lblMsg.Text = "";
            byte[] Byt = new byte[2048];
            //ReceiveSock.Receive(Byt);
            lblMsg.Text = Encoding.ASCII.GetString(Byt);
        }
    }

    private void btn_Send_Click(object sender, EventArgs e)
    {
        Thread SendThread = new Thread(new ThreadStart(SendMsg));
        SendThread.IsBackground = true;
        SendThread.Start();
    }

    private void btnlist_Click(object sender, EventArgs e)
    {
        //Thread ListThread = new Thread(new ThreadStart(Configure));
        //ListThread.IsBackground = true;
        //ListThread.Start();
    }
    private void SendMsg()
    {
        while (true)
        {
            try
            {
                foreach (object SockObj in ClientTable.Keys)
                {
                    byte[] Tosend = new byte[2048];
                    Socket s = (Socket)ClientTable[SockObj];
                    Tosend = Encoding.ASCII.GetBytes("FirstValue&" + GenerateRandom.Next(6, 10).ToString());
                    s.Send(Tosend);
                    //ReceiveSock.Send(Tosend);
                    Thread.Sleep(300);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
最佳回答

您可以修改<代码>Hashtable,Dictionary,List,或在您重新接手时,无论在同一个或另一个方面,也作类似的改动。 该网络4中同时收集了这方面的资料,但我假定你不再使用。 NET 4 (出于兴趣,你为什么仍然使用<代码>Hashtable而不是通用<代码>Dictionary?)

http://www.un.org/Depts/DGACM/index_french.htm

解决这一问题的最简单办法是:

  • Create a new readonly variable used for locking
  • 在《哈希姆报》前插入一栏:

    lock (tableLock)
    {
        ClientTable.Add(ReceiveSock.RemoteEndPoint.ToString(), ReceiveSock);
    }
    
  • 当你希望复制时,在24小时内,在<代码>Hashtable上创建新的<>copy数据。

  • Iterate over the copy instead of the original table

页: 1 它看起来我喜欢一个简单的<代码>List<T>或ArrayList,每个条目要么是袖珍的,要么可能是包含袖珍的习惯类型,要么是你所需要的任何其他信息。 似乎你没有在桌旁进行任意调查。

问题回答

<>Yes>。 Don t do that

The bigger problem here is unsafe multi-threading. The most basic "answer" is just to say: use a synchronization lock on the shared object. However this hides a number of important aspects (like understanding what is happening) and isn t a real solution to this problem in my mind.





相关问题
Anagram Hash Function

I know something like this has been asked before, but the answer was sort of side tracked. I want to develop a hash function which will take a word and spit out an address of an array. So, for ...

Frequently Used metadata Hashmap

Are there any implementations of a static size hashtable that limits the entries to either the most recently or most frequently used metadata? I would prefer not to keep track of this information ...

热门标签