代码之家  ›  专栏  ›  技术社区  ›  Kenshinaro

循环浏览日历的所有约会项时出现问题

  •  0
  • Kenshinaro  · 技术社区  · 6 年前

    我需要从日历中获取所有AppointmentItems,但是在几个项目之后,我得到了一个异常,错误是打开的项目的最大数量是有限的。我不通过foreach循环来循环它们,所以引用应该没有问题。我立即将这些项目“转换”为我的项目类型,以便释放对象。在释放对象并尝试调用垃圾收集器之后,我还将对象的引用设置为null,但错误仍然发生。

    Items items = application.Session.GetFolderFromID(MainCalendar).Items;
    for(Int32 i = 1; i < items.Count; i++)
    {
        AppointmentItem appointmentItem = items[i] as AppointmentItem;
        TempOutlookAppointment tempApp = new TempOutlookAppointment();
        tempApp.FillFromAppointmentItem(appointmentItem);
        appointmentItem.Close(OlInspectorClose.olDiscard);
        Marshal.ReleaseComObject(appointmentItem);
        appointmentItem = null;
        GC.Collect();
        // Adding tempApp to a List
    }
    
    
    
    // FillFromAppointmentItem
    public void FillFromAppointmentItem(Microsoft.Office.Interop.Outlook.AppointmentItem appointmentItem)
    {
            AllDayEvent = appointmentItem.AllDayEvent;
            Body = appointmentItem.Body;
            End = appointmentItem.End;
            Location = appointmentItem.Location;
            OutlookLinkID = appointmentItem.UserProperties.Find("LINKID") != null
                ? Guid.Parse(appointmentItem.UserProperties.Find("LINKID").Value.ToString())
                : Guid.Empty;
            Start = appointmentItem.Start;
            if (appointmentItem.IsRecurring)
            {
                Microsoft.Office.Interop.Outlook.RecurrencePattern pattern = appointmentItem.GetRecurrencePattern();
                TerminSerie serie = new TerminSerie()
                {
                    MaxOccurences = pattern.Occurrences,
                    PatternDayOfMonth = pattern.DayOfMonth,
                    PatternFrequency = pattern.RecurrenceType == Microsoft.Office.Interop.Outlook.OlRecurrenceType.olRecursDaily ? PatternFrequency.Daily : pattern.RecurrenceType == Microsoft.Office.Interop.Outlook.OlRecurrenceType.olRecursMonthly ? PatternFrequency.Monthly : pattern.RecurrenceType == Microsoft.Office.Interop.Outlook.OlRecurrenceType.olRecursWeekly ? PatternFrequency.Weekly : PatternFrequency.Yearly,
                    PatternInterval = pattern.RecurrenceType == Microsoft.Office.Interop.Outlook.OlRecurrenceType.olRecursMonthly ? pattern.Interval / 12 : pattern.Interval,
                    PatternMonthOfYear = pattern.MonthOfYear,
                    RangeEndDate = pattern.PatternEndDate,
                    RangeLimit = pattern.NoEndDate ? RangeLimit.NoLimit : pattern.Occurrences > 0 ? RangeLimit.ByOccurences : RangeLimit.ByDate,
                    RangeStartDate = pattern.PatternStartDate
                };
                Int32 i = pattern.DayOfWeekMask.GetHashCode();
                List<System.DayOfWeek> days = new List<System.DayOfWeek>();
                if (i >= 64)
                {
                    days.Add(System.DayOfWeek.Saturday);
                    i -= 64;
                }
                if (i >= 32)
                {
                    days.Add(System.DayOfWeek.Friday);
                    i -= 32;
                }
                if (i >= 16)
                {
                    days.Add(System.DayOfWeek.Thursday);
                    i -= 16;
                }
                if (i >= 8)
                {
                    days.Add(System.DayOfWeek.Wednesday);
                    i -= 8;
                }
                if (i >= 4)
                {
                    days.Add(System.DayOfWeek.Tuesday);
                    i -= 4;
                }
                if (i >= 2)
                {
                    days.Add(System.DayOfWeek.Monday);
                    i -= 2;
                }
                if (i == 1)
                {
                    days.Add(System.DayOfWeek.Sunday);
                }
                serie.PatternDaysOfWeek = days;
            }
            Subject = appointmentItem.Subject;
        }
    

    2 回复  |  直到 5 年前
        1
  •  1
  •   Kenshinaro    6 年前

    5天来,我不明白为什么这不起作用,现在我得到了一个解决方案。

    Marshal.ReleaseComObject(appointmentItem);
    

    对于这个:

    while(Marshal.ReleaseComObject(appointmentItem) > 0)
        Marshal.ReleaseComObject(appointmentItem);
    

    我现在不知道为什么,但它现在起作用了。

        2
  •  0
  •   Dmitry Streblechenko    6 年前

    仍然有一个隐式变量。更改以下内容

    AppointmentItem appointmentItem = items[i] as AppointmentItem;
    ...
    Marshal.ReleaseComObject(appointmentItem);
    

    object item = items[i];
    AppointmentItem appointmentItem = item as AppointmentItem;
    ...
    Marshal.ReleaseComObject(appointmentItem);
    Marshal.ReleaseComObject(item);