471,337 Members | 1,129 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Prevents Events From Interrupting Events

Hello:

Is there a way to prevent one event from firing while another event is
already being fired?

I have a tool that extracts media from web pages and it has multiple
events firing when the status of the download changes.

Some of the events are used to tell the next file to download while
others manager other resources. However, on occasion, one event will
fire while the other is in the middle of a lock. Since the event hits
a locked resource, it cannot move and I get a deadlock.

What I would really like to do is have the events queue up and run one
at a time. I am thinking my problem is my design decision in the
beginning to avoid an extra thread by using events to handle download
progress. However, I would like to know if queueing threads is even
possible.

Thanks,
Travis

Aug 20 '07 #1
4 1867
Think on event channeling structure (an event manager) which will manage the
events and have a sync object between the event raising.
You can manage list of event , manage the registration/ unregistration and
invocation of them.

--
Sincerely
Yaron Karni
http://dotnetbible.blogspot.com/
"je**********@gmail.com" wrote:
Hello:

Is there a way to prevent one event from firing while another event is
already being fired?

I have a tool that extracts media from web pages and it has multiple
events firing when the status of the download changes.

Some of the events are used to tell the next file to download while
others manager other resources. However, on occasion, one event will
fire while the other is in the middle of a lock. Since the event hits
a locked resource, it cannot move and I get a deadlock.

What I would really like to do is have the events queue up and run one
at a time. I am thinking my problem is my design decision in the
beginning to avoid an extra thread by using events to handle download
progress. However, I would like to know if queueing threads is even
possible.

Thanks,
Travis

Aug 20 '07 #2
je**********@gmail.com wrote:
Is there a way to prevent one event from firing while another event is
already being fired?
No, not really. If you have complete control over the event, you could
add logic to the point in the code where the event is raised. But since
that's pretty obvious and since you're still asking the question, I'm
assuming you don't have complete control over the events (presumably
they are implemented in other classes you use but did not write).
I have a tool that extracts media from web pages and it has multiple
events firing when the status of the download changes.

Some of the events are used to tell the next file to download while
others manager other resources. However, on occasion, one event will
fire while the other is in the middle of a lock. Since the event hits
a locked resource, it cannot move and I get a deadlock.
Who caused the deadlock? Is your code the one that is using the locked
resources? If so, then you also need to design your architecture so
that you don't have one thread trying to get a resource another already
has while that other thread is trying to get the resource the first
thread already has.

If the code that caused the deadlock is outside your control, the author
of that code needs to fix it.
What I would really like to do is have the events queue up and run one
at a time. I am thinking my problem is my design decision in the
beginning to avoid an extra thread by using events to handle download
progress. However, I would like to know if queueing threads is even
possible.
I don't understand your comment that using events in some way avoids an
extra thread. Events and threads are orthogonal ideas; one does not
exclude the other, though it's true that if you want to prevent your
event handlers from deadlocking each other, one fix is to _not_ have
multiple threads.

How is it you believe that using an event has avoided a thread?

There are ways to ensure that execution of specific code is done in a
synchronous manner. The delegate type does in fact make this
potentially easier, since you can either create your own queue of
delegates with an object array for the parameters and process it
yourself, or take advantage of the Control.BeginInvoke() method to queue
execution of delegates on a very specific thread (the thread that owns
the control). But these methods aren't specific to events; you would
have to incorporate them into your own event handlers explicitly.

A better approach would be to understand why you are deadlocking and to
fix the design error in your code that led to that in the first place.

Pete
Aug 20 '07 #3
On Aug 20, 11:31 am, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.com>
wrote:
jehugalea...@gmail.com wrote:
Is there a way to prevent one event from firing while another event is
already being fired?

No, not really. If you have complete control over the event, you could
add logic to the point in the code where the event is raised. But since
that's pretty obvious and since you're still asking the question, I'm
assuming you don't have complete control over the events (presumably
they are implemented in other classes you use but did not write).
I have a tool that extracts media from web pages and it has multiple
events firing when the status of the download changes.
Some of the events are used to tell the next file to download while
others manager other resources. However, on occasion, one event will
fire while the other is in the middle of a lock. Since the event hits
a locked resource, it cannot move and I get a deadlock.

Who caused the deadlock? Is your code the one that is using the locked
resources? If so, then you also need to design your architecture so
that you don't have one thread trying to get a resource another already
has while that other thread is trying to get the resource the first
thread already has.

If the code that caused the deadlock is outside your control, the author
of that code needs to fix it.
What I would really like to do is have the events queue up and run one
at a time. I am thinking my problem is my design decision in the
beginning to avoid an extra thread by using events to handle download
progress. However, I would like to know if queueing threads is even
possible.

I don't understand your comment that using events in some way avoids an
extra thread. Events and threads are orthogonal ideas; one does not
exclude the other, though it's true that if you want to prevent your
event handlers from deadlocking each other, one fix is to _not_ have
multiple threads.

How is it you believe that using an event has avoided a thread?

There are ways to ensure that execution of specific code is done in a
synchronous manner. The delegate type does in fact make this
potentially easier, since you can either create your own queue of
delegates with an object array for the parameters and process it
yourself, or take advantage of the Control.BeginInvoke() method to queue
execution of delegates on a very specific thread (the thread that owns
the control). But these methods aren't specific to events; you would
have to incorporate them into your own event handlers explicitly.

A better approach would be to understand why you are deadlocking and to
fix the design error in your code that led to that in the first place.

Pete
I got away from another thread by having events manage the download
process. In other words, I have an event set the next file to
download.

I am starting to think, however, that I should create another thread
to handle the downloading. This will help reduce some of the
complexity of overlapping event handlers and booleans.

Thanks for your response. By the way . . . is it possible for the same
thread to deadlock itself with the help of events? or is the lock
keyword ignored when it is the same thread?

Aug 20 '07 #4
je**********@gmail.com wrote:
I got away from another thread by having events manage the download
process. In other words, I have an event set the next file to
download.
That doesn't answer the question. Just having an event or an event
handler does not in and of itself avoid the use of a thread. In fact,
in many scenarios events are implemented as a way for a thread to easily
execute code that needs to execute when certain things happen in that
thread.
I am starting to think, however, that I should create another thread
to handle the downloading. This will help reduce some of the
complexity of overlapping event handlers and booleans.
Adding another thread will also add to the complexity of your design.
Since your design already has a deadlock condition as it is, I would be
surprised if you can fix that basic design issue by adding a new thread
and you could easily just make it harder to fix.
Thanks for your response. By the way . . . is it possible for the same
thread to deadlock itself with the help of events? or is the lock
keyword ignored when it is the same thread?
Thread synchronization isn't ignored when it occurs multiple times in
the same thread. However, it is nested without any problem or potential
for a thread deadlocking itself. So within the same thread, you can use
the lock() statement or similar synchronization mechanism multiple times
without any trouble.

For the best answers, you should put together a concise-but-complete
example of code that reliably reproduces the problem you're having and
post that here. Without that, it's very difficult to comment
intelligently on the specifics of your design and how you would best
avoid the deadlock issue.

Pete
Aug 20 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Michael Hill | last post: by
8 posts views Thread by andreas | last post: by
1 post views Thread by Zamdrist | last post: by
1 post views Thread by swethak | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.