我需要从日历中获取所有AppointmentItems,但是在添加了几个Item之后,我遇到了一个异常,错误是说打开的项目数量是有限的。我不会通过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;
}
希望您能帮助我解决我的问题。
答案 0 :(得分:1)
5天,我只是不明白为什么它不起作用,现在我得到了一个随机的解决方案。
对于那些有相同问题的人:我只需要更改此行:
Marshal.ReleaseComObject(appointmentItem);
对此:
while(Marshal.ReleaseComObject(appointmentItem) > 0)
Marshal.ReleaseComObject(appointmentItem);
我现在不为什么,但现在可以使用。
答案 1 :(得分:0)
您仍然有一个隐式变量。更改以下内容
MyDataset
到
AppointmentItem appointmentItem = items[i] as AppointmentItem;
...
Marshal.ReleaseComObject(appointmentItem);