Help!

To use Marshal.ReleaseComObject or not to use

 
  

Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> Program Add-Ins RSS
Next:  Quick access toolbar in inspector window  
Author Message
j
External


Since: Aug 26, 2009
Posts: 2



PostPosted: Wed Aug 26, 2009 4:21 am    Post subject: To use Marshal.ReleaseComObject or not to use
Archived from groups: microsoft>public>outlook>program_addins (more info?)

Hi,

I have shared addIn (.net 2.0, c#)

I add custom fields to mailItem object.
Please have a look in a code snippet below:

......
.......
foreach (XmlNode field in fieldList)
{
fieldName = GetFieldName(field);
fieldValue = GetFieldValue(field);
try
{
AddCustomField(mailItem, fieldName, fieldValue);
}
catch (Exception ex)
{ // log }
}

.....
void AddCustomField(Outlook.MailItem mailItem, string fieldName,
string fieldValue)
{
Outlook.UserProperties ups = mailItem.UserProperties;
SetUserProperty(ups, fieldName, typeof(string), fieldValue);//
adds field to mailItem

Marshal.ReleaseComObject(ups);
ups = null;
}


The question is:
I add a few custom fields to mailItem in a loop, do i need to release
ups (Outlook.UserProperties)?
Can this to destroy com object (mailItem) ??

Thanks in advance.
Back to top
j
External


Since: Aug 26, 2009
Posts: 2



PostPosted: Wed Aug 26, 2009 7:08 am    Post subject: Re: To use Marshal.ReleaseComObject or not to use [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Aug 26, 4:40 pm, "Ken Slovak - [MVP - Outlook]"
<kenslo....TakeThisOut@mvps.org> wrote:
> Releasing "ups" will not release the mail item, only the UserProperties
> object.
>
> If you're doing this in a loop then I would use Marshal.ReleaseComObject()
> so you know when the objects are released. If this is being done only a few
> times I wouldn't bother since the objects will be garbage collected at some
> point in time after the procedure where they are in scope ends.
>
> You always need to balance the perf hit of using Marshal.ReleaseComObject()
> and GC.Collect() against the need to know that certain objects have been
> released or to prevent the build-up of objects counting against the Exchange
> open RPC channel limits.
>
> --
> Ken Slovak
> [MVP - Outlook]http://www.slovaktech.com
> Author: Professional Programming Outlook 2007.
> Reminder Manager, Extended Reminders, Attachment Options.http://www.slovaktech.com/products.htm
>
> "j" <evgeny....TakeThisOut@gmail.com> wrote in message
>
> news:643344bd-ed69-4746-b7fd-5b8cd74447d0@32g2000yqj.googlegroups.com...
>
>
>
> > Hi,
>
> > I have shared addIn (.net 2.0, c#)
>
> > I add custom fields to mailItem object.
> > Please have a look in a code snippet below:
>
> > .....
> > ......
> > foreach (XmlNode field in fieldList)
> > {
> >      fieldName = GetFieldName(field);
> >      fieldValue = GetFieldValue(field);
> >      try
> >      {
> >             AddCustomField(mailItem, fieldName, fieldValue);
> >      }
> >      catch (Exception ex)
> >     {   // log }
> > }
>
> > ....
> > void  AddCustomField(Outlook.MailItem mailItem, string fieldName,
> > string fieldValue)
> > {
> >     Outlook.UserProperties ups = mailItem.UserProperties;
> >      SetUserProperty(ups, fieldName, typeof(string), fieldValue);//
> > adds field to mailItem
>
> >      Marshal.ReleaseComObject(ups);
> >      ups = null;
> > }
>
> > The question is:
> > I add a few custom fields to mailItem in a loop, do i need to release
> > ups (Outlook.UserProperties)?
> > Can this to destroy com object (mailItem) ??
>
> > Thanks in advance.- Hide quoted text -
>
> - Show quoted text -


Thanks,

Of cause i understand that releasing "ups" will not relese the
mailItem.
So i wanted to know whether i need to release the "ups"???, btw after
the loop i also releasing the mailItem.

What do you say??
Back to top
Ken Slovak - [MVP - Outlo
External


Since: Oct 17, 2003
Posts: 3355



PostPosted: Wed Aug 26, 2009 9:40 am    Post subject: Re: To use Marshal.ReleaseComObject or not to use [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Releasing "ups" will not release the mail item, only the UserProperties
object.

If you're doing this in a loop then I would use Marshal.ReleaseComObject()
so you know when the objects are released. If this is being done only a few
times I wouldn't bother since the objects will be garbage collected at some
point in time after the procedure where they are in scope ends.

You always need to balance the perf hit of using Marshal.ReleaseComObject()
and GC.Collect() against the need to know that certain objects have been
released or to prevent the build-up of objects counting against the Exchange
open RPC channel limits.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"j" <evgeny.br.DeleteThis@gmail.com> wrote in message
news:643344bd-ed69-4746-b7fd-5b8cd74447d0@32g2000yqj.googlegroups.com...
> Hi,
>
> I have shared addIn (.net 2.0, c#)
>
> I add custom fields to mailItem object.
> Please have a look in a code snippet below:
>
> .....
> ......
> foreach (XmlNode field in fieldList)
> {
> fieldName = GetFieldName(field);
> fieldValue = GetFieldValue(field);
> try
> {
> AddCustomField(mailItem, fieldName, fieldValue);
> }
> catch (Exception ex)
> { // log }
> }
>
> ....
> void AddCustomField(Outlook.MailItem mailItem, string fieldName,
> string fieldValue)
> {
> Outlook.UserProperties ups = mailItem.UserProperties;
> SetUserProperty(ups, fieldName, typeof(string), fieldValue);//
> adds field to mailItem
>
> Marshal.ReleaseComObject(ups);
> ups = null;
> }
>
>
> The question is:
> I add a few custom fields to mailItem in a loop, do i need to release
> ups (Outlook.UserProperties)?
> Can this to destroy com object (mailItem) ??
>
> Thanks in advance.
Back to top
Ken Slovak - [MVP - Outlo
External


Since: Oct 17, 2003
Posts: 3355



PostPosted: Wed Aug 26, 2009 4:22 pm    Post subject: Re: To use Marshal.ReleaseComObject or not to use [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

If "ups" has not been released yet by the time you try to release the parent
mail item then releasing the parent mail item will fail (not with an error,
but by not releasing that object). So you have to judge that in your
decision.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"j" <evgeny.br DeleteThis @gmail.com> wrote in message
news:4904af04-4f3d-4a6d-b3d1-e08b9b891ddc@o15g2000yqm.googlegroups.com...
<snip>
Thanks,

Of cause i understand that releasing "ups" will not relese the
mailItem.
So i wanted to know whether i need to release the "ups"???, btw after
the loop i also releasing the mailItem.

What do you say??
Back to top
Display posts from previous:   
Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> Program Add-Ins All times are: Eastern Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum