线程安全:已添加具有相同密钥的项目

时间:2019-07-13 20:21:46

标签: c# multithreading thread-safety

我有以下代码,它接受字节数组的输入,并在将原始JSON字符串分解为字节之前重建该字符串。

我收到一条错误消息,提示已添加具有相同密钥的项目。

 public class HandleAccessResponse
    {

        private static byte[] tempStore;
        private static Dictionary<String, int> authchunkID = new Dictionary<String, int>();
        private static ResponseModel responseModel = new ResponseModel();
        private static object lockObj = new object();

        public static ResponseModel HandleResponse(byte[] response)
        {
            byte[] temp;
            byte[] pos = new byte[4];
            byte[] chunk_num = new byte[4];
            byte[] original = new byte[4];
            byte[] guid = new byte[32];

            int position, chunkNumber, ogSize;
            int start = 0, end = 0;
            int chunkSize = 0;

            string theGuid;

            if (response == null)
            {
                Console.WriteLine("Request not set");

            }

            chunkSize = response.Length;

            for (var a = 0; a < 4; a++)
            {
                pos[a] = response[a];
            }

            if (pos == null)
            {
                throw new ArgumentNullException("Position not set");
            }
            else
            {
                position = BitConverter.ToInt32(pos, 0);
            }

            int count = 0;
            for (var b = 4; b < 8; b++)
            {
                chunk_num[count] = response[b];
                count++;
            }

            if (chunk_num == null)
            {
                throw new ArgumentNullException("Chunk number not set");
            }
            else
            {
                chunkNumber = BitConverter.ToInt32(chunk_num, 0);
            }

            int count2 = 0;
            for (var c = 8; c < 12; c++)
            {
                original[count2] = response[c];
                count2++;
            }

            if (original == null)
            {
                throw new ArgumentNullException("Original size not passed");
            }
            else
            {
                ogSize = BitConverter.ToInt32(original, 0);
            }

            // retrieve the guid in the last four bytes
            int count3 = 0;
            for (var d = 12; d < 44; d++)
            {
                guid[count3] = response[d];
                count3++;
            }

            if (guid == null)
            {
                throw new ArgumentNullException("Guid not set");
            }
            else
            {
                theGuid = Encoding.ASCII.GetString(guid);
            }

            lock (lockObj)
            {
                if (isSaved(theGuid))
                {
                    if (incrementCount(authchunkID, theGuid))
                    {
                    }
                    else
                    {   
                    }
                }
                else
                {                 
                    authchunkID.Add(theGuid, 1);

                    responseModel.chunkID.Add(theGuid, 1);

                    tempStore = new byte[ogSize];
                }
            }


            temp = new byte[response.Length - 44];


            Array.Copy(response, 44, temp, 0, temp.Length);

            int x = value_of_x(position);

            if (x == 0)
            {
                temp.CopyTo(tempStore, start);
            }
            else
            {
                int subtract = (x * 4036);
                end = subtract;

                temp.CopyTo(tempStore, end);
            }


            if (authchunkID[theGuid] == chunkNumber)
            {



                var authoriseRspMsg = Encoding.ASCII.GetString(tempStore, 0, tempStore.Length);



                var authoriseRsp = JsonConvert.DeserializeObject<AuthoriseResp>(authoriseRspMsg);


                if (authoriseRsp.transaction != null)
                {

                    authoriseRespModel.authoriseResp = authoriseRsp;
                }
                else
                {
                    Console.WriteLine("Authorise message is empty");
                }
                authchunkID.Remove(theGuid);
            }

            return responseModel;
        }
        private static Boolean incrementCount(Dictionary<string, int> aeschunkID, String guid)
        {
            Boolean isUpdated = false;

            if (aeschunkID.ContainsKey(guid))
            {
                int value = aeschunkID[guid];
                value++;

                aeschunkID[guid] = value;
                responseModel.chunkID[guid] = value;
                isUpdated = true;

                return isUpdated;
            }

            return isUpdated;

        }
        private static Boolean isSaved(String guid)
        {       
                Boolean hasBeen = false;
                if (guid.Length > 0 && !(guid == null))
                {
                    if (authchunkID.ContainsKey(guid))
                    {
                        hasBeen = true;
                    }
                    return hasBeen;
                }
                return hasBeen;            
        }

        private static int value_of_x(int position)
        {
            int x;
            if (position == 0)
            {
                x = 0;
                return x;
            }
            else if (position == 1)
            {
                x = position;
                return x;
            }
            else
            {
                x = position;
                return x;
            }
        }
    }

该代码首次运行,然后在几秒钟后中断,并显示一条错误消息,提示已添加具有相同密钥的项目。我尝试将锁添加到使用“ lockObj ”插入的部分,但这似乎不起作用

0 个答案:

没有答案