Hi,

I an reading a txt file line by line and putting it in to a Queue<string> q = new Queue<string>();
when i execute the program I get a Argument Exception 'Source array was not long enough. Check srcIndex and length, and the array's lower bounds.' how do i handle it

i need to implement the program to that the txt reads and put it into a FIFO queue.

how to i do this?

appreicate a reply
thnks

Post the code that does the reading and adding to the Queue. Point out the line that is giving the error.

It would help if you showed your code. Also would help if you identified the line of code that throws the error.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PF
{
    class Pipe1
    {
        private static Queue<String> _inData1;

        public Pipe1()
        {
            _inData1 = new Queue<String>();
        }
        public void dataIN(String a)
        {
            **_inData1.Enqueue(a);**
        }

        public String dataOUT()
        {
            string retVal = string.Empty;

            if (_inData1.Count > 0)
            {
                retVal = _inData1.Dequeue();
            }

            return retVal;
        }

    }
}

the line that is surrounded by astrix

How long is the text file? I added over a million 80 character strings to a Queue<T> and had no issues (stopped at a million because I was bored). What, exactly, is the error and what is the call stack at the time?

i think the problem is because of threads i guess,

  A a1 = new A(null,p1);  

                C c1 = new C(p2,p3);  
                //D d1 = new D(p3,null);  

                Thread th1 = new Thread( a1.readDate );  
                Thread th2 = new Thread( b1.removeNonAlpha );  


                th1.Start();
                    th2.Start();

so i create 2 threads th1 and th2, th1 thread put in data to the queue and th2 gets data from the queue.

P2 and p3 and classes that implement queue methods.

i think it is the consumer producer problem right

how do i solve it

appreciate a reply

thanks

Queue's are not thread safe, so you should be using locks. Pretty sure I gave you an example in a previous post. Was there anything about it you didn't understand?

hi nmaillet

i used the post that you have linked to

but the error still keeps coming, any idea?

Show your code, how can we debug code we can't see?

true
main.cs

Pipe p = new Pipe();
            Pipe1 p1 = new Pipe1();

            // A a1 = new A(null,p1) null refer to inPipe, and p1 refer to outPipe  

            A a1 = new A(null, p1);
            B b1 = new B(p1, p2);

            Thread th1 = new Thread(a1.readDate);   //Initialize the thread that uses the run method
            Thread th2 = new Thread(b1.removeNonAlpha);

            th1.Start(); //start the thread
            th2.Start();

pipe.cs

 private Queue<String> _inData = new Queue<String>();

        public Pipe()
        {

        }
        public void dataIN(String a)
        {
            lock (_inData)
            {
                _inData.Enqueue(a);
            }
        }

        public String dataOUT()
        {
            string retVal = string.Empty;

            if (_inData.Count > 0)
            {
                retVal = _inData.Dequeue();
            }
            return retVal;
        }

        public int pCount()
        {
            return _inData.Count;
        }

A.cs

   private static Pipe pin;
        private static Pipe1 pout;

        public A(Pipe _in, Pipe1 _out)
        {
            pin = new Pipe();
            pout = new Pipe1();
            pin = _in;
            pout = _out;
        }

        public string getData()
        {
            return pin.dataOUT();
        }

        public void sendData(string tempData)
        {
            pout.dataIN(tempData);
        }

        public void readDate()
        {
            foreach (var textLine in File.ReadAllLines("C:\\SE480\\DataFiles\\Text4.txt"))
            {
                sendData(textLine);
            }
            sendData("Poison");           
        }

this is the code? from A.cs sendData() when it call the Pipe.cs metho it gives the error

appreciate a reply
thanks

You need to make sure you have a lock around every Enqueue() and Dequeue() call. Any instance method/field/property of the Queue class is not thread safe. This also includes checking the Count property, without releasing the lock before an Enqueue() or Dequeue() (that depends on the Count).

hey nmaillet,
so i locked the dequeue and the enque mwthods with the lock, but the error still comes
error msg "Source array was not long enough. Check srcIndex and length, and the array's lower bounds." i comes in the line _inData1.Enqueue(a); when i try to put data to it

so the calling methos is: sendData(s.toLoweer())

public void Remove()
        {

            bool read = false;

            while (true)
            {
                read = false;

                if (pcount() > 0)
                    read = true;

                if (read)
                {
                    string value = getData();

                    if (value != "Poison")
                    {
                        if (value != null)
                        {
                            string[] split = value.Split(new Char[] { ' ' });

                            foreach (string s in split)
                            {


if (Regex.IsMatch(s.ToLower(), @"\A[a-z]+\z"))
                                    {


                                        sendData(s.ToLower());

                                    }



                            }
                        }
                        else
                        {
                            //do something to wait for input
                        }
                    }
                    else
                    {
                        sendData("Poison");
                        break;
                    }
                }
            }
}

is it something else that makes the ArgumentException????

appreciate a reply

thnak you

Not that I've ever seen... and a quick search of Google only seems to turn up the same thing. Could you repost your Pipe and Pipe1 classes? Is there anywhere else that the Queue is referenced (double check all your code please)? If so, post it. It may help to see your changes, and ensure locking is done correctly. Also, an Enqueue() call that is properly locked could still throw the exception if other areas in your code don't lock on the same object properly.

pipe1.cs

 class Pipe1
    {
        private static Queue<String> _inData1;

        public Pipe1()
        {
            _inData1 = new Queue<String>();
        }
        public void dataIN(String a)
        {
            lock (_inData1)
            {
                _inData1.Enqueue(a);
            }
        }

        public String dataOUT()
        {
            string retVal = string.Empty;

            if (_inData1.Count > 0)
            {
                retVal = _inData1.Dequeue();
            }
            return retVal;
        }

        public int p1Count()
        {
            lock (_inData1)
            {
                return _inData1.Count;
            }
        }
    }

Pipe2.cs

 class Pipe2
    {
        private Queue<String> _inData2;

        public Pipe2()
        {
            _inData2 = new Queue<String>();
        }

        public void dataIN(String a)
        {
           lock(_inData2)
            {
                _inData2.Enqueue(a);  
            }           
        }

        public String dataOUT()
        {
            string retVal = string.Empty;

            if (_inData2.Count > 0)
            {
                retVal = _inData2.Dequeue();
            }

            return retVal;
        }

        public int p2Count()
        {
            lock(_inData2){
            return _inData2.Count;
            }
        }
    }

pipe1 and pip2 are both the same functionalities

appreciate a reply
thanks

You need a lock around Dequeue as well. Include The if statement too, since you wouldn't want the Count changing before the Dequeue.

i think i got it
thank you, i will debug the program and let you know

yes this is working

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.