Group:  Microsoft Outlook ยป microsoft.public.outlook.program_addins
Thread: Help! Inspector.Close is fired before Inspector.Activate handler finishes

Geek News

Help! Inspector.Close is fired before Inspector.Activate handler finishes
Sergey Anchipolevsky <windev[ at ]nospam.nospam> 2/3/2006 10:51:34 AM
Hello all.

I've been developing a C# Addin for OL2002/2003 and encountered a strange
problem.

Sometimes inspector's Close event is fired before the Activate handler finishes.
This can be reproduced by opening many inspectors and then closing them sequentially
as fast as possible. Logs show that Close handler is invoked before the Activate
handler finishes. Both handlers are executed in the same thread. As you can
imagine, this causes numerous problems, since the Close handler frees resources
allocated on Inspector creation.

First I thought that the reason might be an invokation of the message loop
(by calling Application.DoEvents() for example) hidden somewhere inside Activate
handler or its callees. But a thorough code analysis showed this is not the
case (at least in the addin). However the Activate handler makes many calls
to the Outlook object model.

I've made some protection from this situation: I don't free any resources
(that is normally done on Close) until Activate handler finishes. But it
looks like Outlook sometimes releases its internal objects before Close event
is fired, so Activate event handler cannot continue - any access to Outlook
object model causes either a null reference exception or a "COM wrapper"
exception.

Does anybody know the real cause of that? Does my addin do something wrong,
or is it an Outlook's "feature"?

The only workaround that comes in mind is creating proxies for Outlook objects
to use them in Activate event handler (as well as in all other handlers).
These proxies would check if the inspector is "closed" before any real operations.
But this decision is quite costly.

Is there a simpler solution?

Any help would be really appreciated.

Thanks in advance.

WBR,
Sergey Anchipolevsky

Re: Help! Inspector.Close is fired before Inspector.Activate handler finishes
"Dmitry Streblechenko" <dmitry[ at ]dimastr.com> 2/3/2006 5:59:39 PM
If your code in the Activate event handler does anything that can
potentially run the Windows message loop (such sa displaying a message box
or any other window), then Close event can potentially fire while Activate
is still beign processed. What do you do in the Activate event handler?

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool

"Sergey Anchipolevsky" <windev[ at ]nospam.nospam> wrote in message
news:3474f2e22f6b48c7f6e014ba08cc[ at ]msnews.microsoft.com...
[Quoted Text]
> Hello all.
>
> I've been developing a C# Addin for OL2002/2003 and encountered a strange
> problem.
>
> Sometimes inspector's Close event is fired before the Activate handler
> finishes. This can be reproduced by opening many inspectors and then
> closing them sequentially as fast as possible. Logs show that Close
> handler is invoked before the Activate handler finishes. Both handlers are
> executed in the same thread. As you can imagine, this causes numerous
> problems, since the Close handler frees resources allocated on Inspector
> creation.
>
> First I thought that the reason might be an invokation of the message loop
> (by calling Application.DoEvents() for example) hidden somewhere inside
> Activate handler or its callees. But a thorough code analysis showed this
> is not the case (at least in the addin). However the Activate handler
> makes many calls to the Outlook object model.
>
> I've made some protection from this situation: I don't free any resources
> (that is normally done on Close) until Activate handler finishes. But it
> looks like Outlook sometimes releases its internal objects before Close
> event is fired, so Activate event handler cannot continue - any access to
> Outlook object model causes either a null reference exception or a "COM
> wrapper" exception.
>
> Does anybody know the real cause of that? Does my addin do something
> wrong, or is it an Outlook's "feature"?
>
> The only workaround that comes in mind is creating proxies for Outlook
> objects to use them in Activate event handler (as well as in all other
> handlers). These proxies would check if the inspector is "closed" before
> any real operations. But this decision is quite costly.
>
> Is there a simpler solution?
>
> Any help would be really appreciated.
>
> Thanks in advance.
>
> WBR,
> Sergey Anchipolevsky
>
>


Re: Help! Inspector.Close is fired before Inspector.Activate handler finishes
"Josh Einstein" <josheinstein[ at ]hotmail.com> 2/5/2006 5:40:41 PM
He should be able to find out exactly what is causing the re-entrance by
setting a breakpoint in the Close event handler and then looking at the
stack trace to see what the Activate event handler calls into that leads to
Close.

From what I understand, there are alot of API's that can cause re-entrance
bugs by pumping the message queue that you might not be aware of such as
waiting on waithandles, sleeping threads, etc. There's a ton of information
about this problem over on cbrummes web log.
http://blogs.msdn.com/cbrumme/archive/2004/02/02/66219.aspx

--
Josh Einstein
Einstein Technologies
Microsoft Tablet PC MVP
Tablet Enhancements for Outlook 2.0 - Try it free for 14 days
www.tabletoutlook.com


"Dmitry Streblechenko" <dmitry[ at ]dimastr.com> wrote in message
news:eFeUptOKGHA.1728[ at ]TK2MSFTNGP14.phx.gbl...
[Quoted Text]
> If your code in the Activate event handler does anything that can
> potentially run the Windows message loop (such sa displaying a message box
> or any other window), then Close event can potentially fire while Activate
> is still beign processed. What do you do in the Activate event handler?
>
> Dmitry Streblechenko (MVP)
> http://www.dimastr.com/
> OutlookSpy - Outlook, CDO
> and MAPI Developer Tool
>
> "Sergey Anchipolevsky" <windev[ at ]nospam.nospam> wrote in message
> news:3474f2e22f6b48c7f6e014ba08cc[ at ]msnews.microsoft.com...
>> Hello all.
>>
>> I've been developing a C# Addin for OL2002/2003 and encountered a strange
>> problem.
>>
>> Sometimes inspector's Close event is fired before the Activate handler
>> finishes. This can be reproduced by opening many inspectors and then
>> closing them sequentially as fast as possible. Logs show that Close
>> handler is invoked before the Activate handler finishes. Both handlers
>> are executed in the same thread. As you can imagine, this causes numerous
>> problems, since the Close handler frees resources allocated on Inspector
>> creation.
>>
>> First I thought that the reason might be an invokation of the message
>> loop (by calling Application.DoEvents() for example) hidden somewhere
>> inside Activate handler or its callees. But a thorough code analysis
>> showed this is not the case (at least in the addin). However the Activate
>> handler makes many calls to the Outlook object model.
>>
>> I've made some protection from this situation: I don't free any resources
>> (that is normally done on Close) until Activate handler finishes. But it
>> looks like Outlook sometimes releases its internal objects before Close
>> event is fired, so Activate event handler cannot continue - any access to
>> Outlook object model causes either a null reference exception or a "COM
>> wrapper" exception.
>>
>> Does anybody know the real cause of that? Does my addin do something
>> wrong, or is it an Outlook's "feature"?
>>
>> The only workaround that comes in mind is creating proxies for Outlook
>> objects to use them in Activate event handler (as well as in all other
>> handlers). These proxies would check if the inspector is "closed" before
>> any real operations. But this decision is quite costly.
>>
>> Is there a simpler solution?
>>
>> Any help would be really appreciated.
>>
>> Thanks in advance.
>>
>> WBR,
>> Sergey Anchipolevsky
>>
>>
>
>


Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net