Browse Source

修改线程安全问题

yinyujing 4 years ago
parent
commit
180d525006
1 changed files with 22 additions and 10 deletions
  1. 22 10
      NB_IOT_TCP_HP_SOCKET/MainForm_TCP.cs

+ 22 - 10
NB_IOT_TCP_HP_SOCKET/MainForm_TCP.cs

@@ -369,7 +369,10 @@ namespace NB_IOT_TCP_HP_SOCKET
                     }
                     else
                     {
-                        deveiceConnId[psp.SourceNumber] = connId;
+                        lock (deveiceConnId)
+                        {
+                            deveiceConnId[psp.SourceNumber] = connId;
+                        }
                         clientInfo.SourceNumber = psp.SourceNumber;
                         clientInfo.DestNumber = psp.DestNumber;
                         PSProtocol protocol2;
@@ -455,9 +458,11 @@ namespace NB_IOT_TCP_HP_SOCKET
 
                 if (deveiceConnId != null && deveiceConnId.Count > 0)
                 {
-                    //KeyValuePair<string, IntPtr>[] nowDeveiceConnId = new KeyValuePair<string, IntPtr>[deveiceConnId.Count];
-                    Dictionary<string, IntPtr> nowDeveiceConnId = new Dictionary<string, IntPtr>(deveiceConnId);
-                    //deveiceConnId.ToArray().CopyTo(nowDeveiceConnId, 0);
+                    Dictionary<string, IntPtr> nowDeveiceConnId = new Dictionary<string, IntPtr>();
+                    lock (deveiceConnId)
+                    {
+                        nowDeveiceConnId = new Dictionary<string, IntPtr>(deveiceConnId);
+                    }
 
 
                     KeyValuePair<string, IntPtr>[] keyValues = nowDeveiceConnId.ToArray();
@@ -469,7 +474,10 @@ namespace NB_IOT_TCP_HP_SOCKET
                         {
                             if (deveiceConnId.Count > 0 && deveiceConnId.ContainsKey(conn.Key))
                             {
-                                deveiceConnId.Remove(conn.Key);
+                                lock (deveiceConnId)
+                                {
+                                    deveiceConnId.Remove(conn.Key);
+                                }
                                 break;
                             }
 
@@ -619,11 +627,13 @@ namespace NB_IOT_TCP_HP_SOCKET
                         if (deveiceConnId != null && deveiceConnId.Count > 0)
                         {
 
-                            Dictionary<string, IntPtr> nowDeveiceConnId = new Dictionary<string, IntPtr>(deveiceConnId);
+                            Dictionary<string, IntPtr> nowDeveiceConnId = new Dictionary<string, IntPtr>();
+                            lock (deveiceConnId)
+                            {
+                                nowDeveiceConnId = new Dictionary<string, IntPtr>(deveiceConnId);
+                            }
 
                             KeyValuePair<string, IntPtr>[] keyValues = nowDeveiceConnId.ToArray();
-                            //nowDeveiceConnId = new KeyValuePair<string, IntPtr>[deveiceConnId.Count];
-                            //deveiceConnId.ToArray().CopyTo(nowDeveiceConnId , 0);
 
                             // 处理设备对应的链接,如果超时则断开链接并移除设备-链接对应关系
                             foreach (KeyValuePair<string, IntPtr> conn in keyValues)
@@ -664,7 +674,10 @@ namespace NB_IOT_TCP_HP_SOCKET
                         {
                             if (deveiceConnId != null && deveiceConnId.Count > 0 && deveiceConnId.ContainsKey(key))
                             {
-                                deveiceConnId.Remove(key);
+                                lock (deveiceConnId)
+                                {
+                                    deveiceConnId.Remove(key);
+                                }
                             }
                         }
 
@@ -812,7 +825,6 @@ namespace NB_IOT_TCP_HP_SOCKET
                         // 处理数据
                         ClientInfo clientInfo = (ClientInfo)_SendConidQueue.Dequeue();
                         PSProtocol psp = clientInfo.pSProtocol;
-                        //clientInfo.NewConnId = deveiceConnId[psp.SourceNumber];
                         // 原地址
                         string sourceNumber = psp.SourceNumber;