Solved

Queue Dequeue ArrayList Count 0?

Posted on 2011-02-10
8
659 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 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 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Technology Partners: 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!

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

696 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