0

I am trying to use lock for read and write to list at the same time. I adding bytes from serial port to a list while processing these bytes by reading from same list with the help of lock. My problem is that while running it in debug mode it is working fine but while running normally it showing exception. Following is my code.

    private void receiveData(object sender, SerialDataReceivedEventArgs e)
    {
        while (connectComPort.BytesToRead > 0)
                receivedBytes1.Add((byte)connectComPort.ReadByte());
    }

    int tint = 0;
    private static readonly object _object = new object();
    private void modprocessReceivedBuffer()
    {
        while (1 == 1)
        {
            try
            {
                if (receivedBytes1.Count() > tint)
                {
                    List<byte> receivedBytes12 = null;
                    lock (_object)
                    {
                        receivedBytes12 = receivedBytes1.GetRange(tint, 2).ToList<byte>(); //LINE 1
                    }
                    if (receivedBytes12[0] == 0x0D)
                    {
                        if (receivedBytes12[1] == 0xAF)
                        {
                            try
                            {
                                    var tiff = receivedBytes1.GetRange(tint, ((int)receivedBytes1[tint + 4])).ToList<byte>();
                                    tint += (int)receivedBytes1[tint + 4];
                                    modifiedProcess(tiff);
                                    if (receivedBytes1.Count() == tint)
                                    {
                                        receivedBytes1.Clear();
                                        tint = 0;
                                        Thread.Sleep(20);
                                    }
                            }
                            catch
                            {
                            }

                        }
                    }
                    else
                    {
                        tint += 1;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("bug : " + ex.ToString());
            }
        }
    }

Line 1 shows "System.ArgumentException: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection" while running the code normally. I am not able to catch this error while running in debug mode. My second question is that if their is any thread safe list like structure in c#? I would also like to add that I am receiving data from port at a very fast rate 16000 bytes/sec average.

prattom
  • 1,625
  • 11
  • 42
  • 67
  • 3
    Use `ConcurrentBag` as thread-safe list. – Patrick Hofman Jan 04 '16 at 12:47
  • 1
    Bag is not a list: `SynchronizedCollection` is a `IList` – Mikey Jan 04 '16 at 12:55
  • Indeed, useful reading: [What is the difference between SynchronizedCollection and the other concurrent collections?](http://stackoverflow.com/a/4655236/993547). – Patrick Hofman Jan 04 '16 at 13:06
  • Most likely, `receivedBytes1` doesn't have enough data for your processing - note that you also get the exact line where the exception happened if you include the symbol file (yourAssembly.pdb). Basically, you need to ensure there's at least `tint + 1` bytes in the buffer - *and* you need to add a whole lot of other synchronization (e.g. there's places where you access `receivedBytes1` with no synchronization at all). The truth is, you probably want to use some kind of an asynchronous stream, rather than `List<...>`. – Luaan Jan 04 '16 at 13:06
  • 1
    You also need to lock when *adding* to `receivedBytes1`, of course. If you're new to multi-threading, you might want to start with http://www.albahari.com/threading/ :) It's quite typical that synchronization issues pop up very rarely in testing and debugging, since they typically arise in complicated timing scenarios. You need to know how to write safe multi-threaded code if you want to use multi-threading at all :) – Luaan Jan 04 '16 at 13:13

0 Answers0