none
Looping ModAppointment RRS feed

  • Question

  • I have a ModAppointment listener, which seems to constantly be attempting to Mod the same appointment, after creating and saving it this is also giving me issues when loading up Outlook. This is the Event Listener

    public class CalendarMonitor { public event EventHandler<EventArgs<Outlook.AppointmentItem>> AppointmentModified; private Outlook.NameSpace _session; private List<string> listFolderPaths; private List<Outlook.MAPIFolder> listCalendarFolders; private List<Outlook.Items> listCalendarItems; private Outlook.MAPIFolder fldrDeletedItems; public CalendarMonitor(Outlook.NameSpace aSession) { listFolderPaths = new List<string>(); listCalendarFolders = new List<Outlook.MAPIFolder>(); listCalendarItems = new List<Outlook.Items>(); // Store namespace globally to ensure the same instance is used in future calls. _session = aSession; // Retrieve deleted items folder fldrDeletedItems = aSession.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDeletedItems); // Retrieve calendar folder Outlook.MAPIFolder fldrCalendar = _session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar); if (fldrCalendar != null) SetupCalendarEvents(fldrCalendar); } // Save and maintain appointment events private void SetupCalendarEvents(Outlook.MAPIFolder aCalendarFolder) { if (aCalendarFolder.DefaultItemType != Outlook.OlItemType.olAppointmentItem) { MessageBox.Show("An error has occured. Please contact helpdesk quoting \"The MAPIFolder must use AppointmentItems as the default type.\""); } // To stop event handler duplication, each appointment when set with event handlers will be saved to a list. // Event handlers are only added to 'untouched' appointments. if (listFolderPaths.Contains(aCalendarFolder.FolderPath) == false) { Outlook.Items items = aCalendarFolder.Items; // Store folder path to prevent listener duplication listFolderPaths.Add(aCalendarFolder.FolderPath); // Store a reference to the folder and to the items collection so that it remains alive. This keeps the ref count up on the underlying COM object and prevents // it from being intermittently released (then the events don't get fired). listCalendarFolders.Add(aCalendarFolder); listCalendarItems.Add(items); // Add event handlers for the events we need. items.ItemChange += new Outlook.ItemsEvents_ItemChangeEventHandler(CalendarItems_ItemChange); } } // Triggered when a calendar item is changed private void CalendarItems_ItemChange(object anItem) { try { if (anItem is Outlook.AppointmentItem) { if (this.AppointmentModified != null) { this.AppointmentModified(this, new EventArgs<Outlook.AppointmentItem>((Outlook.AppointmentItem)anItem)); } } } catch (Exception ex) { } finally { if (Marshal.IsComObject(anItem)) { Marshal.FinalReleaseComObject(anItem); anItem = null; } } }
    }

    private void ModAppointment(object sender, EventArgs<Outlook.AppointmentItem> e)

    {}

    Tuesday, June 19, 2012 11:38 AM

Answers

  • looks like case i described previously. So either keep a flag or use suggestion from Ken - keep list with EntryIds that you already processed so you won't stay in endless loop of item_change->raise_event->event_handler_modifies_appointment->item_change_event_raised_again and so on and so on untill you deplete your stack

    Wednesday, June 20, 2012 8:04 AM

All replies

  • well, there is chance of infinite lop (stack overflow exception) because you are rising event with item from within itemchange event, so if some subscriber does something with item, event will be rauised again and again and again. Also you store mapifolders unnecessirly, Items object is sufficient.

    Tuesday, June 19, 2012 11:50 AM
  • Could you explain a little bit more? I didn't code this part, so don't fully understand it I'm just trying to stop it modifiying the same appointment.  We are trying to catch the Modified Event, to amend our database with the changes.  What would be the better way of doing this then?

    Thanks

    Tuesday, June 19, 2012 3:56 PM
  • maybe let's start from different end - please describe in full detail (steps to reproduce) exactly what errors do you get.
    Tuesday, June 19, 2012 6:27 PM
  • No particular errors, it was just in this particular instance Outlook was taking an age to load and when I broke into the code it was looping through the modified event again and again, but I can't work out why as I had previously saved it before.
    Wednesday, June 20, 2012 7:39 AM
  • looks like case i described previously. So either keep a flag or use suggestion from Ken - keep list with EntryIds that you already processed so you won't stay in endless loop of item_change->raise_event->event_handler_modifies_appointment->item_change_event_raised_again and so on and so on untill you deplete your stack

    Wednesday, June 20, 2012 8:04 AM