none
Issue with VB.NET 2010 Garbage Collection RRS feed

  • Question

  • Hi guys,

    I managed to get an Outlook calendar automation VB program to work in general, only with issues from which I wonder may have to do with garbage collection. My application will always run ok and without errors but the appointment items in my Outlook calendar don't always get updated as desired. I noticed that the more times I run the code on the local system without restarting, the less chance the appointment items get updated. I used Garbage Collector methods to try to tackle this problem but to no avail it seems. If this info helps, the Outlook icon decalring that "the program is in use by another" doesn't ever close after a few brief debugging attempts. I'd be grateful for your help on this.

    Many thanks,

    Jared.

    Thursday, September 13, 2012 12:02 PM

Answers

  • I'd add a Logon() call for NameSpace after you get it as you're not running in-process in an addin:
     

        Dim _missing As System.Reflection.Missing.Value

        oOutlookNamespace.Logon("", "", _missing, _missing)
     
    Marshal is a member of System.Runtime.InteropServices
     
        Imports System.Runtime.InteropServices
     
    Marshal.ReleaseComObject(oOutlookNameSpace)
     
    or use Marshal.FinalReleaseComObject().
     
    I'd also release the Outlook.Application object before calling the GC.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "jjb1989" <=?utf-8?B?ampiMTk4OQ==?=> wrote in message news:ed2b78b5-f7a4-4a61-bd38-a6a4e35fa4b9...

    Hi Ken,

    Here's a an OO method code from my application:

    Public Sub CreateLeave()
            Dim oOutlook As New Outlook.Application
            Dim oOutlookNamespace As Microsoft.Office.Interop.Outlook.NameSpace = oOutlook.GetNamespace("MAPI")
            Dim oAppItem As Outlook.AppointmentItem = oOutlook.CreateItem(Outlook.OlItemType.olAppointmentItem)
            oAppItem.Subject = "Annual Leave"
            oAppItem.Start = fromDate
            oAppItem.End = toDate.AddDays(1)
            oAppItem.ReminderSet = True
            oAppItem.BusyStatus = Outlook.OlBusyStatus.olOutOfOffice
            oOutlookNamespace = Nothing
            oAppItem.Save()
            oAppItem = Nothing
            'Cleanup
            GC.Collect()
            GC.WaitForPendingFinalizers()
            GC.Collect()
            GC.WaitForPendingFinalizers()
            oOutlook.Quit()
        End Sub

    I tried the Marshal.ReleaseComObject() but could not find it listed. Is it perhaps because I'm using WPF?

    Thanks,

    Jared.


    Ken Slovak MVP - Outlook
    • Marked as answer by jjb1989 Monday, September 17, 2012 11:09 AM
    Thursday, September 13, 2012 8:19 PM
    Moderator

All replies

  • Are you saving items after changing them?
     
    Are you actually releasing items by calling Marshal.ReleaseComObject() on the items when you no longer need them, followed by setting the item = Nothing?:
     
    What is your code? Show a small piece of it where you are having problems.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "jjb1989" <=?utf-8?B?ampiMTk4OQ==?=> wrote in message news:ddc6eef9-658e-40c3-a178-82b772baab7d...

    Hi guys,

    I managed to get an Outlook calendar automation VB program to work in general, only with issues from which I wonder may have to do with garbage collection. My application will always run ok and without errors but the appointment items in my Outlook calendar don't always get updated as desired. I noticed that the more times I run the code on the local system without restarting, the less chance the appointment items get updated. I used Garbage Collector methods to try to tackle this problem but to no avail it seems. If this info helps, the Outlook icon decalring that "the program is in use by another" doesn't ever close after a few brief debugging attempts. I'd be grateful for your help on this.

    Many thanks,

    Jared.


    Ken Slovak MVP - Outlook
    Thursday, September 13, 2012 2:47 PM
    Moderator
  • Hi Ken,

    Here's a an OO method code from my application:

    Public Sub CreateLeave()
            Dim oOutlook As New Outlook.Application
            Dim oOutlookNamespace As Microsoft.Office.Interop.Outlook.NameSpace = oOutlook.GetNamespace("MAPI")
            Dim oAppItem As Outlook.AppointmentItem = oOutlook.CreateItem(Outlook.OlItemType.olAppointmentItem)
            oAppItem.Subject = "Annual Leave"
            oAppItem.Start = fromDate
            oAppItem.End = toDate.AddDays(1)
            oAppItem.ReminderSet = True
            oAppItem.BusyStatus = Outlook.OlBusyStatus.olOutOfOffice
            oOutlookNamespace = Nothing
            oAppItem.Save()
            oAppItem = Nothing
            'Cleanup
            GC.Collect()
            GC.WaitForPendingFinalizers()
            GC.Collect()
            GC.WaitForPendingFinalizers()
            oOutlook.Quit()
        End Sub

    I tried the Marshal.ReleaseComObject() but could not find it listed. Is it perhaps because I'm using WPF?

    Thanks,

    Jared.

    Thursday, September 13, 2012 3:04 PM
  • I'd add a Logon() call for NameSpace after you get it as you're not running in-process in an addin:
     

        Dim _missing As System.Reflection.Missing.Value

        oOutlookNamespace.Logon("", "", _missing, _missing)
     
    Marshal is a member of System.Runtime.InteropServices
     
        Imports System.Runtime.InteropServices
     
    Marshal.ReleaseComObject(oOutlookNameSpace)
     
    or use Marshal.FinalReleaseComObject().
     
    I'd also release the Outlook.Application object before calling the GC.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "jjb1989" <=?utf-8?B?ampiMTk4OQ==?=> wrote in message news:ed2b78b5-f7a4-4a61-bd38-a6a4e35fa4b9...

    Hi Ken,

    Here's a an OO method code from my application:

    Public Sub CreateLeave()
            Dim oOutlook As New Outlook.Application
            Dim oOutlookNamespace As Microsoft.Office.Interop.Outlook.NameSpace = oOutlook.GetNamespace("MAPI")
            Dim oAppItem As Outlook.AppointmentItem = oOutlook.CreateItem(Outlook.OlItemType.olAppointmentItem)
            oAppItem.Subject = "Annual Leave"
            oAppItem.Start = fromDate
            oAppItem.End = toDate.AddDays(1)
            oAppItem.ReminderSet = True
            oAppItem.BusyStatus = Outlook.OlBusyStatus.olOutOfOffice
            oOutlookNamespace = Nothing
            oAppItem.Save()
            oAppItem = Nothing
            'Cleanup
            GC.Collect()
            GC.WaitForPendingFinalizers()
            GC.Collect()
            GC.WaitForPendingFinalizers()
            oOutlook.Quit()
        End Sub

    I tried the Marshal.ReleaseComObject() but could not find it listed. Is it perhaps because I'm using WPF?

    Thanks,

    Jared.


    Ken Slovak MVP - Outlook
    • Marked as answer by jjb1989 Monday, September 17, 2012 11:09 AM
    Thursday, September 13, 2012 8:19 PM
    Moderator
  • Thanks Ken,

    The Marshal methods seem to have done the trick!

    Jared.

    Monday, September 17, 2012 11:09 AM