Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
Is this posible? Will it be stable?
C#
namespace A1
{
    public class A
    {
        private static Queue<object> q = new Queue<object>(); //??????????
        public static void EnQueue(object obj)
        {
            q.Enqueue(obj);
        }
        public static objedt DeQueue()
        {
            return q.Dequeue();
        }
        public static object PeekFirst()
        {
            return q.Peek();
        }
    }
}
namespace B1
{
    public class B
    {
        //use class A.
        internal void DoWork()
        {
            //do some work here.
            A1.A.EnQueue();//?????
            //do some work here.
            A1.A.PeekFirst();//?????
            A1.A.DeQueue();//?????
        }
    }
}
Posted
Updated 18-Dec-10 20:35pm
v2
Comments
TweakBird 19-Dec-10 2:35am    
Fixed <pre> tag.
Abhinav S 19-Dec-10 5:20am    
Er...what exactly do you mean by 'stable'?
pluss400 19-Dec-10 5:44am    
what is put in is not comming out.
OriginalGriff 19-Dec-10 6:48am    
Answer updated

I can't see why it should not be possible.

As for stability, everything should work fine as long as you use only one thread to access the queue. The Queue class is not thread safe by default. Quote from MSDN:

Public static (Shared in Visual Basic) members of this type are thread safe.
Any instance members are not guaranteed to be thread safe.
A Queue(T) can support multiple readers concurrently, as long as the collection is not modified. (...)
 
Share this answer
 
Comments
pluss400 19-Dec-10 5:08am    
Thank you.
I have some strange results that only can be my Queue. If I take this a step further and make the Queue thread safe with "class ReaderWriterLockSlim". Then using the static Queue to shovel data between the 2 clases with one thread in each class that handles the data. Should that be a problem?
Best to you.
Per
Yes, it would work (provided you fix the compilation problems - "objedt" becomes "object", "DoWork" should use the results from "A".)

It's a messy solution though. Try not to use statics if you possibly can.
"Than you.
My intension is to use the Queue system, shoveling data between 2 Projects in the same solution. In Project 1. The receiver side there are 2 classes depenging on this Queue. In Project 2, the giver side, one class puting data in Queue.
Do you have a better solution to the problem, Please, Im all ears off course.
I want to solve this problem with one instance, serving all projects and classes.
Best to you.
"


Oooh! No - it won't work!
If you have two projects, then you have two processes - even if they are in the same solution. Each process has it's own memory space - they do not share. So even if you have a static variable in a class, if you have two processes they do not share the same static variable.

To put it another way: If you put data on the static queue in a process, you must remove it in the same process (even if it is on a different thread). You cannot remove it from a different processes static queue.

Instead, look at Inter-process communciations - sockets may be a little heavy handed here but they may be the easiest way to do it!
See Here[^] for a quick tutorial.
 
Share this answer
 
v2
Comments
pluss400 19-Dec-10 5:20am    
Than you.
My intension is to use the Queue system, shoveling data between 2 Projects in the same solution. In Project 1. The receiver side there are 2 classes depenging on this Queue. In Project 2, the giver side, one class puting data in Queue.
Do you have a better solution to the problem, Please, Im all ears off course.
I want to solve this problem with one instance, serving all projects and classes.
Best to you.
Per
Yes it us quite possible. You might actually try and use the singleton design pattern to achieve something similar.
 
Share this answer
 
Comments
pluss400 19-Dec-10 5:38am    
Thanks. What is the singleton design pattern?
pluss400 19-Dec-10 5:55am    
Ok. Read about Singelton. Thats what I have tryed to do all the time. Hove do you devide the Singel instance(Queue object) between Projects and their classes? Do I go about this the wrong way. If so tell mee hove?
Best to you
Per
pluss400 19-Dec-10 8:20am    
Thanks again. Youst what I was looking for. Found a total explanation on this sigt.
http://csharpindepth.com/Articles/General/Singleton.aspx
Best to you.
Per
Abhinav S 19-Dec-10 9:15am    
I'm glad I could help. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900