Skip to content

Commit adc1b05

Browse files
committed
A couple changes:
1. Don't prompt for the profile. Outlook will use the default if that's how it is configured. 2. Free up Outlook resources to not keep the Outlook process running on startup or when we sync.
1 parent 5a7fc6f commit adc1b05

2 files changed

Lines changed: 54 additions & 15 deletions

File tree

src/OutlookCalendarSync/MainForm.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,24 @@ public MainForm()
4545
numericUpDownDaysInThePast.Value = Settings.Instance.DaysInThePast;
4646
numericUpDownDaysInTheFuture.Value = Settings.Instance.DaysInTheFuture;
4747
textBoxMinuteOffsets.Text = Settings.Instance.MinuteOffsets;
48-
OutlookHelper oh = new OutlookHelper();
48+
OutlookHelper oh = null;
49+
50+
try
51+
{
52+
oh = new OutlookHelper();
53+
}
54+
catch (COMException)
55+
{
56+
//Just retry once
57+
oh = new OutlookHelper();
58+
}
59+
4960
checkedListBoxCalendars.Items.Clear();
5061
List<OutlookCalendar> savedCalendarsToSync = new List<OutlookCalendar>();
5162
savedCalendarsToSync.AddRange(Settings.Instance.CalendarsToSync.ToArray());
5263
foreach (OutlookCalendar calendar in oh.CalendarFolders)
5364
{
54-
int position = checkedListBoxCalendars.Items.Add(calendar);
65+
int position = checkedListBoxCalendars.Items.Add(calendar.Name);
5566
foreach (OutlookCalendar entry in savedCalendarsToSync)
5667
{
5768
if (entry.Name.Equals(calendar.Name))
@@ -61,6 +72,8 @@ public MainForm()
6172
}
6273
}
6374

75+
freeCOMResources(oh);
76+
6477
checkBoxSyncEveryHour.Checked = Settings.Instance.SyncEveryHour;
6578
checkBoxShowBubbleTooltips.Checked = Settings.Instance.ShowBubbleTooltipWhenSyncing;
6679
checkBoxStartInTray.Checked = Settings.Instance.StartInTray;
@@ -163,6 +176,7 @@ private void syncWorker_DoWork(object sender, DoWorkEventArgs e)
163176
if (calendarsToSync.Count < 2)
164177
{
165178
MessageBox.Show("You need at least two calendars to sync on the 'Settings' tab.");
179+
freeCOMResources(oh);
166180
return;
167181
}
168182

@@ -530,12 +544,12 @@ private void checkedListBoxCalendars_ItemCheck(object sender, ItemCheckEventArgs
530544
List<OutlookCalendar> checkedItems = new List<OutlookCalendar>();
531545
foreach (var item in checkedListBoxCalendars.CheckedItems)
532546
{
533-
checkedItems.Add((OutlookCalendar)item);
547+
checkedItems.Add(new OutlookCalendar((string)item, null));
534548
}
535549

536550
if (e.NewValue == CheckState.Checked)
537551
{
538-
checkedItems.Add((OutlookCalendar)checkedListBoxCalendars.Items[e.Index]);
552+
checkedItems.Add(new OutlookCalendar((string)checkedListBoxCalendars.Items[e.Index], null));
539553
}
540554

541555
Settings.Instance.CalendarsToSync = checkedItems;

src/OutlookCalendarSync/OutlookHelper.cs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Runtime.InteropServices;
4+
using System.Threading;
45
//using Outlook = Microsoft.Office.Interop.Outlook;
56
using Microsoft.Office.Interop.Outlook;
67

@@ -11,7 +12,6 @@ namespace OutlookCalendarSync
1112
/// </summary>
1213
public class OutlookHelper
1314
{
14-
//private static OutlookHelper instance;
1515
private List<OutlookCalendar> _calendarFolders = new List<OutlookCalendar>();
1616

1717
private string _accountName = string.Empty;
@@ -32,10 +32,10 @@ public OutlookHelper()
3232
Application oApp = new Application();
3333

3434
// Get the NameSpace and Logon information.
35-
NameSpace oNS = oApp.GetNamespace("mapi");
36-
37-
//Log on by using a dialog box to choose the profile.
38-
oNS.Logon("","", true, true);
35+
NameSpace oNS = (NameSpace)RetryInteropAction(new Func<NameSpace>(() =>
36+
{
37+
return oApp.GetNamespace("mapi");
38+
}));
3939

4040
Store store = oNS.DefaultStore;
4141
_accountName = store.DisplayName;
@@ -50,17 +50,42 @@ public OutlookHelper()
5050
}
5151
else
5252
{
53-
Marshal.ReleaseComObject(folder);
53+
Marshal.FinalReleaseComObject(folder);
5454
}
5555
}
5656

57-
Marshal.ReleaseComObject(mailbox);
57+
Marshal.FinalReleaseComObject(mailbox);
58+
}
59+
60+
Marshal.FinalReleaseComObject(oApp);
61+
Marshal.FinalReleaseComObject(oNS);
62+
oApp = null;
63+
oNS = null;
64+
}
65+
66+
private object RetryInteropAction(Func<object> action)
67+
{
68+
int count = 0;
69+
while (count < 5)
70+
{
71+
try
72+
{
73+
count++;
74+
return action();
75+
}
76+
catch (COMException)
77+
{
78+
if (count < 5)
79+
{
80+
Thread.Sleep(1000);
81+
continue;
82+
}
83+
84+
throw;
85+
}
5886
}
5987

60-
// Done. Log off.
61-
oNS.Logoff();
62-
Marshal.ReleaseComObject(oApp);
63-
Marshal.ReleaseComObject(oNS);
88+
return null;
6489
}
6590
}
6691
}

0 commit comments

Comments
 (0)