The applicationhangs when event is being unhooked

A

Amit Dedhia

Hi

I am developing an application using C++/CLI in Visual Studio 2005. In
my application I have following structure.

(1) Class A creates instances of Class B and Class C.
(2) Class B implements a timer function which repeatedly executes a
timer every 250 ms.
(3) Class C instance hooks for a event raised by Class B instance.
Class B always raises this event asynchronously at the end of timer
function. In the event handler class C raises another event which is
hooked by Class A.
(4) The event handler in class A eventually update the main UI control
(using control->Invoke() which executes on main thread)

Now, Class A's destructor is called from the main application thread.
In the destructor, class A first calls destructor of class B. Class
B's destructor returns after the timer is properly destroyed.
Then class A tries to unhook the class C's event. Here it hangs!!! The
unhook code is standard event unhook code using -= operator.

Can anyone guess why it hangs? In the thread window I saw that the
event handler in class A (for the event raised by class C) was waiting
on control->Invoke() method. However it does not explain to me why the
class A hangs at the point of unhooking an event raised by class C. Is
it reqd that the event handlers must be completely executed before it
can be unhooked? I dont think so. There is definitely a deadlock
situation here. But I am not able to guess it?

Anyone?

Thanks in advance
Amit Dedhia
 
N

Nicholas Paldino [.NET/C# MVP]

Amit,

Well, since it is completely a guess (you haven't shown any code which
would clarify what you are doing), I would say that you should remove the
event handler before the destructor is called. Does that fix your problem?
 

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

Top