A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Add-ins for Outlook
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

To use Marshal.ReleaseComObject or not to use



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old August 26th 09, 12:21 PM posted to microsoft.public.outlook.program_addins
j
external usenet poster
 
Posts: 109
Default To use Marshal.ReleaseComObject or not to use

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.
Ads
  #2  
Old August 26th 09, 02:40 PM posted to microsoft.public.outlook.program_addins
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default To use Marshal.ReleaseComObject or not to use

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" wrote in message
...
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.


  #3  
Old August 26th 09, 03:08 PM posted to microsoft.public.outlook.program_addins
j
external usenet poster
 
Posts: 109
Default To use Marshal.ReleaseComObject or not to use

On Aug 26, 4:40*pm, "Ken Slovak - [MVP - Outlook]"
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" wrote in message

...



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??
  #4  
Old August 26th 09, 09:22 PM posted to microsoft.public.outlook.program_addins
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default To use Marshal.ReleaseComObject or not to use

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" wrote in message
...
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??

 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 09:18 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.