BackGroundWorkers

  • Thread starter Thread starter MarcuEusebiu
  • Start date Start date
M

MarcuEusebiu

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
 
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
 
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
 
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
 
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 :
 
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 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 :)
 
Thanks Jon... I made it work in only one thread. I've dump the
BackGroundWorkers... and now works fine...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top