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