private void freeCOMResources(OutlookHelper oh) { try { _aiCache.ClearAndReleaseAll(); if (oh != null && oh.CalendarFolders != null) { foreach (OutlookCalendar oc in oh.CalendarFolders) { if (oc != null && oc.Folder != null) { Marshal.FinalReleaseComObject(oc.Folder); } } } GC.Collect(); GC.WaitForPendingFinalizers(); } catch (System.Exception ex) { logboxout("Warning: Error freeing COM resources:\r\n" + ex.ToString()); } }
public MainForm() { InitializeComponent(); labelAbout.Text = labelAbout.Text.Replace("{version}", System.Windows.Forms.Application.ProductVersion); //load settings/create settings file if (File.Exists(FILENAME)) { Settings.Instance = XMLManager.Import <Settings>(FILENAME); } else { XMLManager.Export(Settings.Instance, FILENAME); } //update GUI from Settings numericUpDownDaysInThePast.Value = Settings.Instance.DaysInThePast; numericUpDownDaysInTheFuture.Value = Settings.Instance.DaysInTheFuture; textBoxMinuteOffsets.Text = Settings.Instance.MinuteOffsets; OutlookHelper oh = null; try { oh = new OutlookHelper(); } catch (COMException) { //Just retry once oh = new OutlookHelper(); } checkedListBoxCalendars.Items.Clear(); List <OutlookCalendar> savedCalendarsToSync = new List <OutlookCalendar>(); savedCalendarsToSync.AddRange(Settings.Instance.CalendarsToSync.ToArray()); foreach (OutlookCalendar calendar in oh.CalendarFolders) { int position = checkedListBoxCalendars.Items.Add(calendar.Name); foreach (OutlookCalendar entry in savedCalendarsToSync) { if (entry.Name.Equals(calendar.Name)) { checkedListBoxCalendars.SetItemChecked(position, true); } } } freeCOMResources(oh); checkBoxSyncEveryHour.Checked = Settings.Instance.SyncEveryHour; checkBoxShowBubbleTooltips.Checked = Settings.Instance.ShowBubbleTooltipWhenSyncing; checkBoxStartInTray.Checked = Settings.Instance.StartInTray; checkBoxMinimizeToTray.Checked = Settings.Instance.MinimizeToTray; checkBoxCreateFiles.Checked = Settings.Instance.CreateTextFiles; //set up timer (every 30s) for checking the minute offsets _ogstimer = new Timer(); _ogstimer.Interval = 30000; _ogstimer.Tick += new EventHandler(ogstimer_Tick); _ogstimer.Start(); _oldtime = DateTime.Now; _syncWorker.WorkerReportsProgress = true; _syncWorker.WorkerSupportsCancellation = true; _syncWorker.DoWork += syncWorker_DoWork; _syncWorker.ProgressChanged += syncWorker_ProgressChanged; _syncWorker.RunWorkerCompleted += syncWorker_RunWorkerCompleted; }
private void syncWorker_DoWork(object sender, DoWorkEventArgs e) { if (e.Argument is string && "DELETE".Equals((string)e.Argument)) { deleteAllSyncItems(); return; } DateTime syncStarted = DateTime.Now; OutlookHelper oh = null; try { logboxout("Sync started at " + syncStarted.ToString()); logboxout("--------------------------------------------------"); oh = new OutlookHelper(); List <OutlookCalendar> calendarsToSync = new List <OutlookCalendar>(); foreach (OutlookCalendar calendar in oh.CalendarFolders) { foreach (OutlookCalendar entry in Settings.Instance.CalendarsToSync) { if (entry.Name.Equals(calendar.Name)) { calendarsToSync.Add(calendar); } } } if (calendarsToSync.Count < 2) { MessageBox.Show("You need at least two calendars to sync on the 'Settings' tab."); freeCOMResources(oh); return; } foreach (OutlookCalendar calendarFrom in calendarsToSync) { logboxout("Reading Outlook Calendar entries from Source:\r\n " + calendarFrom.Name); List <AppointmentItemCacheEntry> fromOutlookEntries = new List <AppointmentItemCacheEntry>(); foreach (AppointmentItem a in calendarFrom.GetAppointmentItemsInRange(syncStarted)) { fromOutlookEntries.Add(_aiCache.GetAppointmentItemCacheEntry(a, calendarFrom.Name)); } logboxout("Found " + fromOutlookEntries.Count + " calendar Entries."); foreach (OutlookCalendar calendarTo in calendarsToSync.Where(c => !c.Name.Equals(calendarFrom.Name))) { logboxout("Syncing calendar from Source to Destination:\r\n " + calendarTo.Name); List <AppointmentItemCacheEntry> toOutlookEntries = new List <AppointmentItemCacheEntry>(); foreach (AppointmentItem a in calendarTo.GetAppointmentItemsInRange(syncStarted)) { toOutlookEntries.Add(_aiCache.GetAppointmentItemCacheEntry(a, calendarTo.Name)); } logboxout("Found " + fromOutlookEntries.Count + " Destination calendar Entries."); List <AppointmentItem> itemsToDelete = identifyEntriesToBeDeleted(fromOutlookEntries, toOutlookEntries, calendarFrom.Name); logboxout("Found " + itemsToDelete.Count + " sync items to delete in Destination calendar."); List <AppointmentItem> itemsToCreate = identifyEntriesToBeCreated(fromOutlookEntries, toOutlookEntries); logboxout("Found " + itemsToCreate.Count + " items to create in Destination calendar."); if (itemsToDelete.Count > 0) { logboxout("Deleting " + itemsToDelete.Count + " sync items from Destination calendar..."); foreach (AppointmentItem ai in itemsToDelete) { ai.Delete(); } } if (itemsToCreate.Count > 0) { logboxout("Creating " + itemsToCreate.Count + " items in Destination calendar..."); foreach (AppointmentItem ai in itemsToCreate) { AppointmentItem newAi = calendarTo.Folder.Items.Add(OlItemType.olAppointmentItem) as AppointmentItem; newAi.UserProperties.Add(USER_PROPERTY_NAME, OlUserPropertyType.olText).Value = calendarFrom.Name; newAi.Start = ai.Start; newAi.StartTimeZone = ai.StartTimeZone; newAi.End = ai.End; newAi.EndTimeZone = ai.EndTimeZone; newAi.AllDayEvent = ai.AllDayEvent; newAi.Subject = SUBJECT_PREFIX + ai.Subject; newAi.Location = ai.Location; newAi.BusyStatus = ai.BusyStatus; newAi.Importance = ai.Importance; newAi.Sensitivity = ai.Sensitivity; newAi.RequiredAttendees = ai.RequiredAttendees; newAi.OptionalAttendees = ai.OptionalAttendees; newAi.RTFBody = ai.RTFBody; if (checkBoxAddReminders.Checked && ai.ReminderSet) { newAi.ReminderSet = ai.ReminderSet; newAi.ReminderMinutesBeforeStart = ai.ReminderMinutesBeforeStart; } else { newAi.ReminderSet = false; } newAi.Save(); } } logboxout("Done."); logboxout("--------------------------------------------------"); } } DateTime syncFinished = DateTime.Now; TimeSpan elapsed = syncFinished - syncStarted; logboxout("Sync finished at " + syncFinished.ToString()); logboxout("Time needed: " + elapsed.Minutes + " min " + elapsed.Seconds + " s"); } catch (System.Exception ex) { logboxout("Error Syncing:\r\n" + ex.ToString()); } freeCOMResources(oh); }