Solved

Queue Dequeue ArrayList Count 0?

Posted on 2011-02-10
8
648 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
System.Speech 2 26
Google Directions API to Map URL -C#? 3 20
Graphics 2 27
Hey!!!! 1 20
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

17 Experts available now in Live!

Get 1:1 Help Now