Learned the Hard Way - Outlook Addins and anonymous event handlers

by Joe Enzminger 27. October 2010 12:21

Since I just spent a bunch of time trying to figure out why our Addin was causing Outlook to hang on shutdown:

Do not capture references to Outlook VSTO objects (like Explorer objects), in your anonymous event handler delegates.  Since there is no way to release these, the references to these objects cannot be garbage collected.  Outlook will dutifully wait for this to happen before shutting down, and since it doesn't, it will hang.  

Since you can't count on the Addin.Shutdown event to be fired in Outlook 2010, you must be very careful where you keep references to any Outlook VSTO objects in general.  I would recommend NOT keeping any references. For instance, don't store a reference to the Explorer object in your AddIn class.  Use Application.ActiveExplorer() if you need to get the Explorer object.  

On a more general not about lambda expressions and anonymous methods - use them with care as event handlers.  Unless you jump through a lot of hoops, you can't easily remove them from the handler chain so they will live for as long as the event publisher does.  If that is the lifetime of your application, then any objects captured by your handlers will also live for the life of your application (who says you can't leak memory in .NET).  

I've recently read a lot of articles about WeakEventHandlers, WeakReferences, etc. as it relates to solving this problem.  The "correct" way to solve it is to explicitly remove event handlers when they are no longer used, thus releases the resources those handlers captured.  However, there is no syntactically clean way to do this currently in .NET, and all of the clever ways I've seen written to do it far outway the syntactic advantage of using anonymous methods.

What is needed is a way to get a reference to an anonymous delegate when it is added to the event chain so that you can use it later when you are ready to clean up.  I suspect that most people simply don't worry about cleaning up event handlers.

Right now, it looks like this:

someevent += () => {//Do event work here};

How would you remove this event handler?  Answer, you can't!

Alternatives should be:

AnonymousHandler handler = someevent += () => {//Do event work here};

 

so that later you could:

someevent -= handler;

That's not supported by the language, however, so if you want to use anonymous delegates you pretty much are trading off the ability to elegantly clean up after yourself.  I've always liked the idea of symmetry in code - for every new, a delete (not suppoted in c#, unfortunately), for every Create, a Destroy, for Init, a Kill, for AddRef, a Release. This is an example where the language doesn't support symmetry, and it should be fixed!

  

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

c# Programming | Outlook Programming

Facebook, Social Networks and Privacy - what are you worth?

by Joe Enzminger 27. April 2010 15:21

Part of our recent work has started to steer toward social networking technologies.  While we are working on some techincal areas related to how to manage very large databases of related objects, the work has started me thinking about what value we place on our personal information.

When I was in the gambling industry, I was always amazed at the ridiculousness of the basic contract between a gambler and the casino.  If it was ever written down, it would look something like:

<Insert Casino Name Here> in due consideration of $1.00, promises to pay <Insert Gambler's Name Here> a sum of $.92.

Yet people still gamble?  

If the Federal Government asked you to maintain and report, on a real time basis, a list of all of your friends and family, your personal information (name, address, phone number, email address), employment, and what groups you were interested in, along with snippets of your random thoughts, and oh, by the way, also convinced websites that you visit to help them keep track of your activities on the web, would that be alright with you?

What price do you put on your privacy?  Services like Facebook provide a "free" service that anyone can use.  However, you have to fork over a whole heck of a lot of private information in order to fully participate.  In effect, you are paying that price.  If I came and asked you to give me access to all of your correspondence with your friends, a list of all of your friends, personal information and pictures about yourself, and also wanted to track your web history, what would I have to pay?  Would you do it for $100 per year?  $50?  Certainly you wouldn't sell it for $2 per year.  

You give it away to Facebook.  And they make $2 per year off of it (at least, if you currently believe they make $1B per year).  Their profit, by the way, is much, much less.

I think the kind of information that 400,000,000 people are giving to Facebook is worth more than the value of the service Facebook provides, by far.  You could easily run a profitable subscriber based social network, with no ads and no privacy concerns, at a subscriber cost between $2 and $10 per year.  Think about that.  $1 per month per user, at Facebook subscriber numbers is about $1B+ per year in revenue.  And you get to keep ownership of your private information for yourself.  It seems to me to be a much better deal for the user, but how can you argue with 400,000,000 who have decided otherwise?

 

 

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Facebook Privacy

VMWare Fusion for the Mac

by Joe Enzminger 14. October 2009 14:17

I've been using a Mac for my laptop for some time now (I use it for our iPhone development).  I wish I had discovered it earlier, but VMWare Fusion allows you to run windows on the Mac in a VM.  I just finished getting my development environment up and running on Windows XP on my Mac, so now I can do development outside the office (and run other Windows apps if I need to).

Very cool.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

ScriptManager and Internet Explorer Parsing Errors

by Joe Enzminger 5. October 2009 03:20

So it turns out that even IE8 does not like script that modifies an element before it is closed.  I ran into the issue when trying to implement my own version of AJAX Autocomplete and Context Menus.  For positioning purposes, I want my control containers to be added to the body.  This makes sure they are above all other elements (this gets around having to do the iframe hack) and also makes it easier to position.  The problem is that the ScriptManager for asp.net always adds scripts at the end of the form tag.  this means that any elements added by your script to form or body will generate the IE warning (sometimes its a warning, sometimes its a page error - I haven't figured out what trips the difference.)

jQuery has the $(document).ready().  However, if you aren't using jQuery, set a simple timer to fire your initialization code a few milliseconds after the page is finished loading.  Worked for me.

Joe

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

javascript

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen