By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,993 Members | 1,883 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,993 IT Pros & Developers. It's quick & easy.

How to make sure a method waits to finish before firing again

P: n/a
Hi

I have a situation where a method is fired from receiving a message from my
server.

On receiving this message it triggers a visual animation.

So what happens when the server sends say 3 messages in quick succession.

I want the method to fire, animate completely, and then fire again, finish
completely then fire again.

How do i do this? Do i need to lock the thread the method fires on?

Thanks
Feb 9 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Does your program know when the animation is complete?

Try this:

Message Queue[1000]; // 1000 Messages can be placed in a queue.

void Method( Message X ) // The one receiving messages from the server.
{
if( Queue is not full )
Message -Into the Queue; // At the last position.
else
Oops, need a bigger queue; // Or implement is as a dynamic linked
list instead of an array....
}

void Separate_Thread()
{
while( true )
{
while( Messages exist in the Queue )
{
if( Animation still playing )
Sleep( 1000 );
else
{
First Message -Out of the Queue;
Shift the Queue 1 position to the left;
Start a new Animation;
}
}
}
}

Hope this helps...

"PokerMan" <no****@pokercat.co.ukwrote in message
news:eb**************@TK2MSFTNGP02.phx.gbl...
Hi

I have a situation where a method is fired from receiving a message from
my server.

On receiving this message it triggers a visual animation.

So what happens when the server sends say 3 messages in quick succession.

I want the method to fire, animate completely, and then fire again, finish
completely then fire again.

How do i do this? Do i need to lock the thread the method fires on?

Thanks

Feb 10 '07 #2

P: n/a
Forgot to mention that if the Queue is implemented as an array, you need to
keep synchronization in mind (2 threads reading/writing/shifting the array).

I am not sure you will need to worry about synchronization in case of a
dynamic linked list, since it's safe to just append to the tail by one
thread and remove from the head by another one. No shifting.

"Ashot Geodakov" <a_********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
Does your program know when the animation is complete?

Try this:

Message Queue[1000]; // 1000 Messages can be placed in a queue.

void Method( Message X ) // The one receiving messages from the server.
{
if( Queue is not full )
Message -Into the Queue; // At the last position.
else
Oops, need a bigger queue; // Or implement is as a dynamic linked
list instead of an array....
}

void Separate_Thread()
{
while( true )
{
while( Messages exist in the Queue )
{
if( Animation still playing )
Sleep( 1000 );
else
{
First Message -Out of the Queue;
Shift the Queue 1 position to the left;
Start a new Animation;
}
}
}
}

Hope this helps...

"PokerMan" <no****@pokercat.co.ukwrote in message
news:eb**************@TK2MSFTNGP02.phx.gbl...
>Hi

I have a situation where a method is fired from receiving a message from
my server.

On receiving this message it triggers a visual animation.

So what happens when the server sends say 3 messages in quick succession.

I want the method to fire, animate completely, and then fire again,
finish completely then fire again.

How do i do this? Do i need to lock the thread the method fires on?

Thanks


Feb 10 '07 #3

P: n/a
I've done similar things with the BackGroundWorker component, where the
original form/class was waiting for BGW_Completed to fire.

"PokerMan" wrote:
Hi

I have a situation where a method is fired from receiving a message from my
server.

On receiving this message it triggers a visual animation.

So what happens when the server sends say 3 messages in quick succession.

I want the method to fire, animate completely, and then fire again, finish
completely then fire again.

How do i do this? Do i need to lock the thread the method fires on?

Thanks
Feb 10 '07 #4

P: n/a
Ashot Geodakov <a_********@hotmail.comwrote:
Forgot to mention that if the Queue is implemented as an array, you need to
keep synchronization in mind (2 threads reading/writing/shifting the array).

I am not sure you will need to worry about synchronization in case of a
dynamic linked list, since it's safe to just append to the tail by one
thread and remove from the head by another one. No shifting.
You'd still need to make it thread-safe to make sure two things don't
grab the same item at the same time.

Fortunately, there's no need to implement the queue yourself - that's
what the Queue class is for. You just need to synchronize access to it.

--
Jon Skeet - <sk***@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
Feb 10 '07 #5

P: n/a
So i use this Queue class and it handles the thread safety for me? Or
ithandles the queuing for me and i have to handle any thread safety?

Any examples of how to implement this qeueue?

Thanks

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Ashot Geodakov <a_********@hotmail.comwrote:
>Forgot to mention that if the Queue is implemented as an array, you need
to
keep synchronization in mind (2 threads reading/writing/shifting the
array).

I am not sure you will need to worry about synchronization in case of a
dynamic linked list, since it's safe to just append to the tail by one
thread and remove from the head by another one. No shifting.

You'd still need to make it thread-safe to make sure two things don't
grab the same item at the same time.

Fortunately, there's no need to implement the queue yourself - that's
what the Queue class is for. You just need to synchronize access to it.

--
Jon Skeet - <sk***@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

Feb 10 '07 #6

P: n/a
PokerMan <no****@pokercat.co.ukwrote:
So i use this Queue class and it handles the thread safety for me? Or
ithandles the queuing for me and i have to handle any thread safety?
It handles the queuing, and you handle the thread safety.
Any examples of how to implement this qeueue?
The queue is already implemented. See System.Collections.Queue and
System.Collections.Generic.Queue<T>.

--
Jon Skeet - <sk***@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
Feb 10 '07 #7

P: n/a
Ok thanks i looked up queue and have this in mind:
1) Object serialised and sent by server
2) Object deserialised client side and resulting object is placed in the
queue.
3) A separate thread would run through the qeueue at all times handling any
elements in it. If the grabbed object is still being handled do not move
onto the next one. I assume some kind of event to be raised after any object
handling is done could be caught here to tell it to move to the next element
in the queue?

As you can see, part 3 is where i have some confusion? Any ideas or
tutorials for a similar situation?

My biggest problem is that my current solution is everytime an object is
received i have a event raised to tell me that item is received. And lots of
listeners for all these events. All being trigegred. So if one object comes
in while another is running i get the sync problem. I need some way of
knowing one is being handled and to fire the next one after.

All help greatly appreciated, this part will be fundamental to my app and i
think will fix a mass amount of bugs from the design flaw.

Thank you in advance
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
PokerMan <no****@pokercat.co.ukwrote:
>So i use this Queue class and it handles the thread safety for me? Or
ithandles the queuing for me and i have to handle any thread safety?

It handles the queuing, and you handle the thread safety.
>Any examples of how to implement this qeueue?

The queue is already implemented. See System.Collections.Queue and
System.Collections.Generic.Queue<T>.

--
Jon Skeet - <sk***@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

Feb 10 '07 #8

P: n/a
On Feb 10, 7:25 am, "PokerMan" <nos...@pokercat.co.ukwrote:
Ok thanks i looked up queue and have this in mind:

1) Object serialised and sent by server
2) Object deserialised client side and resulting object is placed in the
queue.
3) A separate thread would run through the qeueue at all times handling any
elements in it. If the grabbed object is still being handled do not move
onto the next one. I assume some kind of event to be raised after any object
handling is done could be caught here to tell it to move to the next element
in the queue?

As you can see, part 3 is where i have some confusion? Any ideas or
tutorials for a similar situation?

My biggest problem is that my current solution is everytime an object is
received i have a event raised to tell me that item is received. And lots of
listeners for all these events. All being trigegred. So if one object comes
in while another is running i get the sync problem. I need some way of
knowing one is being handled and to fire the next one after.

All help greatly appreciated, this part will be fundamental to my app and i
think will fix a mass amount of bugs from the design flaw.
Normally, queueing between threads works like this:

Thread receives event that an item is received. It deserializes the
item, takes out a lock on the queue, and Enqueues the item (using the
Enqueue method).

Thread polling queue for work wakes up, locks the queue, and tries to
Dequeue an element from the queue, then unlocks the queue. If an item
was successfully dequeued, it processes it and then goes back to lock
the queue and dequeue another item. If it fails to dequeue an item
(there is nothing more to do) then it goes to sleep waiting to be
woken up again.

I believe that there is no need to manually lock / unlock the queue if
you don't use it directly, but rather call Queue.Synchronized to get a
synchronized wrapper for it. I've never done that, myself, so someone
else here will have to say exactly how to do that.

So, you see, there's no need to mark items in the queue as "being
processed". You grab the item _off_ the queue and _then_ process it.
Everything in the queue is waiting to be processed, so producing
threads just have to enqueue items and then kick the consuming thread
to wake it up (if it's not awake already).

The only tricky bit is how to wake up the processing thread (or
threads, as you may scale this to have more than one thread processing
elements from the queue). I believe that Jon Skeet's pages on
multithreading contain a standard producer / consumer example for
multithreading. Take a look here:

http://www.yoda.arachsys.com/csharp/threads/

Feb 11 '07 #9

P: n/a
Thanks Bruce,

I already implemented the queuing part and dequeing, used the sync wrapper
as you said.

However i was concerned on how to do the 'wake up' part. Presumably i have
two options here:

a) After queing fire an event, and then dequeue.

My issue here is, that doesn't that remove the point of the queue? Or does
the queue just ensure, as i lock it, that only one item at a time is
processed until complete?

b) Have a thread running on an infinite loop that processes the queue. So
the moment something is added it is processed?

I assume b is not a good option. But option a will run the queue process on
the thread that queued the item. As a result it would block my client from
receiving messages until the process completed.

Maybe an A and B option is best? Fire an event that starts a new thread,
processes the queue and then completes. That way my client continues
receiving messages and adding them to the queue and the queue ensures only
one at a time is processed? Am i right in my thinking here?
Feb 11 '07 #10

P: n/a
On Feb 11, 12:05 pm, "PokerMan" <nos...@pokercat.co.ukwrote:
Thanks Bruce,

I already implemented the queuing part and dequeing, used the sync wrapper
as you said.

However i was concerned on how to do the 'wake up' part. Presumably i have
two options here:

a) After queing fire an event, and then dequeue.

My issue here is, that doesn't that remove the point of the queue? Or does
the queue just ensure, as i lock it, that only one item at a time is
processed until complete?

b) Have a thread running on an infinite loop that processes the queue. So
the moment something is added it is processed?

I assume b is not a good option. But option a will run the queue process on
the thread that queued the item. As a result it would block my client from
receiving messages until the process completed.

Maybe an A and B option is best? Fire an event that starts a new thread,
processes the queue and then completes. That way my client continues
receiving messages and adding them to the queue and the queue ensures only
one at a time is processed? Am i right in my thinking here?
Take a look at Jon's multi-threading pages. I believe that he has an
example of just this kind of scenario: the consumer thread loops
indefinitely, but goes to sleep when it finds the queue empty. When a
producer queues a new item, it "kicks" the consumer thread to wake it
up. If a producer queues an item while the consumer is processing
something, the "kick" doesn't do anything: the consumer checks that
the queue is empty before going to sleep, sees that it isn't, and
grabs the next item to process.

Again, take a look at Jon's write-up. I'm sure I remember reading just
this example there.

Feb 12 '07 #11

P: n/a
Hi,

"Ashot Geodakov" <a_********@hotmail.comwrote in message
news:un**************@TK2MSFTNGP03.phx.gbl...
| Forgot to mention that if the Queue is implemented as an array, you need
to
| keep synchronization in mind (2 threads reading/writing/shifting the
array).
|
| I am not sure you will need to worry about synchronization in case of a
| dynamic linked list, since it's safe to just append to the tail by one
| thread and remove from the head by another one. No shifting.

Of course you need to, what if one thread is removing the only component and
other is trying to add a second one?

--
Ignacio Machin
machin AT laceupsolutions com
Feb 12 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.