遍历日历的所有 AppointmentItems 时出现问题
Problem while looping through all AppointmentItems of a Calendar
我需要从日历中获取所有 AppointmentItems,但是在一些项目之后我得到了一个异常,错误是说打开的项目的最大值是有限的。我不通过 foreach 循环循环它们,因此引用应该没有问题。我立即 "convert" 这些项目进入我的项目类型,以便我可以释放对象。在释放对象后,我还将对象的引用设置为 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;
}
我希望你能帮助我解决我的问题。
5 天我只是不明白为什么这不起作用,现在我随机得到了一个解决方案。
对于那些有同样问题的人:我只需要更改这一行:
Marshal.ReleaseComObject(appointmentItem);
给这个:
while(Marshal.ReleaseComObject(appointmentItem) > 0)
Marshal.ReleaseComObject(appointmentItem);
我现在不知道为什么,但现在可以了。
你还有一个隐式变量。更改以下内容
AppointmentItem appointmentItem = items[i] as AppointmentItem;
...
Marshal.ReleaseComObject(appointmentItem);
至
object item = items[i];
AppointmentItem appointmentItem = item as AppointmentItem;
...
Marshal.ReleaseComObject(appointmentItem);
Marshal.ReleaseComObject(item);
我需要从日历中获取所有 AppointmentItems,但是在一些项目之后我得到了一个异常,错误是说打开的项目的最大值是有限的。我不通过 foreach 循环循环它们,因此引用应该没有问题。我立即 "convert" 这些项目进入我的项目类型,以便我可以释放对象。在释放对象后,我还将对象的引用设置为 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;
}
我希望你能帮助我解决我的问题。
5 天我只是不明白为什么这不起作用,现在我随机得到了一个解决方案。
对于那些有同样问题的人:我只需要更改这一行:
Marshal.ReleaseComObject(appointmentItem);
给这个:
while(Marshal.ReleaseComObject(appointmentItem) > 0)
Marshal.ReleaseComObject(appointmentItem);
我现在不知道为什么,但现在可以了。
你还有一个隐式变量。更改以下内容
AppointmentItem appointmentItem = items[i] as AppointmentItem;
...
Marshal.ReleaseComObject(appointmentItem);
至
object item = items[i];
AppointmentItem appointmentItem = item as AppointmentItem;
...
Marshal.ReleaseComObject(appointmentItem);
Marshal.ReleaseComObject(item);