Solved

Queue Dequeue ArrayList Count 0?

Posted on 2011-02-10
8
645 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
  • 4
  • 2
  • 2
8 Comments
 
LVL 29

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 85

Accepted Solution

by:
Mike Tomlinson earned 250 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 29

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
 

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 250 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 29

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 85

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now