Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C# Programming

BackGroundWorkers


Hello,
I have a Queue and 2 BackGroundWorkers. How can I set one worker to
Peek in Queue and the other one to Dequeue the first element from
queue?

Thanks

On May 30, 8:12 am, MarcuEuse@gmail.com wrote:

> I have a Queue and 2 BackGroundWorkers. How can I set one worker to
> Peek in Queue and the other one to Dequeue the first element from
> queue?

It's not clear exactly what you're having trouble with - sharing the
queue, or making the two worker threads do different things. If you
just specify different delegates for the different workers, it should
be fine (assuming you handle the locking of access to the queue etc).

Jon

-----------------------------------------------Reply-----------------------------------------------

One BGW is peeking and the other one is dequeueing. So they are doing
different things. After dequeueing, the thread that was peeking the
first time, must be the one who peeks now.... Somthing like this..
BGW1 --- P
BGW2 --- D
BGW1 --- P
BGW2 --- D
................
BGW1 --- P
BGW2 --- D

-----------------------------------------------Reply-----------------------------------------------
On May 30, 8:22 am, MarcuEuse@gmail.com wrote:

> One BGW is peeking and the other one is dequeueing. So they are doing
> different things. After dequeueing, the thread that was peeking the
> first time, must be the one who peeks now.... Somthing like this..
> BGW1 --- P
> BGW2 --- D
> BGW1 --- P
> BGW2 --- D
> ................
> BGW1 --- P
> BGW2 --- D

Right, so you provide different delegates for the different workers.
So far so easy - but how are you expecting to keep them alternating as
you've got above?

Jon

-----------------------------------------------Reply-----------------------------------------------

Yes, I have different delegates for each worker. But I don't know how
can it be done... that's why I've post this question in here... I am
looking for a solution... maybe someone has one...

I have this code and GetFirstElement() is called by both workers (the
print method makes a StreamWriter and calls WriteLine):
int a;
object GetFirstElement()
        {

            lock (MyQueue)
            {
                if (aa == 1)
                {
                    this.print("Thread ID=" +
Thread.CurrentThread.ManagedThreadId.ToString());
                    aa = 0;
                    return MyQueue.Dequeue();
                }
                else
                {

                    this.print("Thread ID=" +
Thread.CurrentThread.ManagedThreadId.ToString());
                    ++aa;
                    return MyQueue.Peek();
                }
            }
        }
When I look into the log file(build in print(string message) method),
I get this :
-------------------
Thread ID=11
-------------------
Thread ID=11
-------------------
Thread ID=7
-------------------
Thread ID=7
-------------------
Thread ID=11
-------------------
Thread ID=7...
I want to see : 11,7,11,7,11,711,7.... and so on..

-----------------------------------------------Reply-----------------------------------------------

 <MarcuEuse@gmail.com> wrote:
> Yes, I have different delegates for each worker. But I don't know how
> can it be done... that's why I've post this question in here... I am
> looking for a solution... maybe someone has one...

<snip>

> I want to see : 11,7,11,7,11,711,7.... and so on..

Right - so actually your question isn't really about getting background
workers to do different things, it's about synchronizing their
behaviour. In this case, you can use Monitor.Wait/Monitor.Pulse or a
WaitHandle of some description to signal from one thread to the other
"Finished - your go now".

Are these threads doing a lot of other work though? My first thought
would be to put it all into one thread - the behaviour you've specified
is clearly a lot easier to achieve that way :)

--
Jon Skeet - <s@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

-----------------------------------------------Reply-----------------------------------------------

Thanks Jon... I made it work in only one thread. I've dump the
BackGroundWorkers... and now works fine...
Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc