?
Solved

Queue Dequeue ArrayList Count 0?

Posted on 2011-02-10
8
Medium Priority
?
668 Views
Last Modified: 2012-08-13
I can successfully Enqueue ArrayList's to a Collections.Queue. Having problem Dequeue-ing ArrayList. I know the Queued ArrayLists contain data by running a foreach at myQueue.cs

[myQueue]
 
internal class myQueue
{
   private static Queue que = Queue.Synchronized(new Queue());

   internal static void Enque(ArrayList list)
   {
         que.Enqueue(list);
   }

    internal static ArrayList Deque()
    {
         return (ArrayList)que.Dequeue();
    }  
}

Every attempt to dequeue the queued ArrayList returns a count of 0, although I know the Queue definitely has ArrayList's queued.

[AnotherClass.cs]

 ArrayList arr = new ArrayList(Que.Deque()); //count=0

 ArrayList arr = new ArrayList(); arrData.AddRange(Que.Deque()); //count=0

 ArrayList arr = Que.Deque();//count=0

 ArrayList arr = (ArrayList)Que.Deque();//count=0

 ArrayList arr = new ArrayList();
 arr = Que.Deque();
0
Comment
Question by:pointeman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 34866930
Honestly, I didn't understand, how you created your Queue in anotherclass, and what you have enqueued... Pls more details
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 34866936
It works for me:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ArrayList al = new ArrayList();
            al.Add("one");
            al.Add("two");
            al.Add("three");
            myQueue.Enque(al);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ArrayList al = myQueue.Deque();
            foreach (Object o in al)
            {
                Console.WriteLine(o.ToString());
            }
        }
    }

    internal class myQueue
    {
        private static Queue que = Queue.Synchronized(new Queue());

        internal static void Enque(ArrayList list)
        {
            que.Enqueue(list);
        }

        internal static ArrayList Deque()
        {
            return (ArrayList)que.Dequeue();
        }
    }
}

Open in new window


Show us more complete code please...
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 34867268
Yeah, indeed... How Idle_Mind enques a list - I can see! :)

I believe the error in the original post - no lists were enqued!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:pointeman
ID: 34867372
I though it could be a threading issue, but change the Queue into a String and Dequeue works fine.

More details...

1. Windows Service project, NOT winform.
2. Class1.cs successfully Enqueues data to myQueue.cs
3. myQueue.cs successfully writes ArrayList data to logfile using foreach...
4  Class2 uses myQueue.Deque() Unsuccessfully

[Class1.cs]

ArrayList1 arrayList1 = new ArrayList();
arrayList1.add...........
myQueue.Enque(arrayList1);

[myQueue.cs]
internal class myQueue
{
   private static Queue que = Queue.Synchronized(new Queue());

   internal static void Enque(ArrayList list)
   {
         que.Enqueue(list);
   }

    internal static ArrayList Deque()
    {
         return (ArrayList)que.Dequeue();
    }  
}

[Class2.cs]
ArrayList al = myQueue.Deque();
foreach (Object o in al)
{
    Console.WriteLine(o.ToString());
}

NOT returning any data, just count 0

Maybe this is a Windows Service & threading issue?
0
 
LVL 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 1000 total points
ID: 34867435
Don't think this is threading problem and especially not a win service problem...

I have a suspicion that myQueue refers to different objects in different classes (Class1 and Class2).

You may try try the following trick. Save a myQueue.GetHashCode to a log in both cases. If you work with the same object - hash code will be same. Otherwise - this is the source of the problem.
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 34867441
BTW, how you pass the queue object between classes?
0
 

Author Comment

by:pointeman
ID: 34867703
Good point...

1. I use "MyQueue myQueue = new MyQueue" ONLY 1 time in entire project.
2. I instanciate ALL classes & threads in very 1st boot-up class (via service1 OnStart()).
2. Unusual that I can grab strings all day long, but cannot return the ArrayList.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34867938
Regarding #1, everything in myQueue is STATIC, so you don't need an instance of it.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question