如何解决当我尝试访问默认日历文件夹时,Outlook 插件卡住了
我已经实现了一个执行同步过程的 Outlook 插件。在这里,我使用 Redemption 来访问用户的默认日历文件夹。但是当尝试访问默认日历文件夹时,用户的其中一个 Outlook 卡住了。他们使用的是内部交换服务器,并且他们的默认日历文件夹中只有大约 50 个项目。我为此使用了后台线程。
private void syncWorker_DoWork(object sender,System.ComponentModel.DoWorkEventArgs e)
{
try
{
Helper.DoSyncInBackground();
}
catch (System.Exception ex)
{
Debug.DebugMessage(2,$"Error in adxFormMainPanel (syncWorker_DoWork) {ex.Message}");
}
}
这是我初始化 Redemption 的方法
public static void DoSyncInBackground()
{
try
{
var redemptionCode = new RedemptionCode();
redemptionCode.InitialiseRedemption(Globals.MapiObject);
DoSync(redemptionCode);
}
catch (Exception ex)
{
Debug.DebugMessage(2,$"Error in Helper (DoSyncInBackground) {ex.Message}");
}
}
public bool InitialiseRedemption(object MAPIObject)
{
try
{
Debug.DebugMessage(5,"starts Intialize Redemption");
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
RedemptionLoader_DllLocation64Bit_FilePath = path + "\\Redemption64.dll";
RedemptionLoader_DllLocation32Bit_FilePath = path + "\\Redemption.dll";
Debug.DebugMessage(4,"Redemption FilePath 32:- " + RedemptionLoader_DllLocation32Bit_FilePath);
Debug.DebugMessage(4,"Redemption FilePath 64:- " + RedemptionLoader_DllLocation64Bit_FilePath);
if (File.Exists(RedemptionLoader_DllLocation32Bit_FilePath) && File.Exists(RedemptionLoader_DllLocation64Bit_FilePath))
{
RedemptionLoader.DllLocation32Bit = RedemptionLoader_DllLocation32Bit_FilePath;
RedemptionLoader.DllLocation64Bit = RedemptionLoader_DllLocation64Bit_FilePath;
}
else
{
Debug.DebugMessage(1,AllMessages.MsgRedemptionDllNotFound);
return false;
}
rSession = RedemptionLoader.new_RDOSession();
Debug.DebugMessage(4,"InitialiseRedemption : New RDO session Created");
if (rSession != null)
{
//We need a MAPIOBject from Outlook when we initialise
rSession.MAPIOBJECT = MAPIObject;
bool loggedOn = rSession.LoggedOn;
string rMode = rSession.ExchangeConnectionMode.ToString();
RDOAddressEntry currentUser = rSession.CurrentUser;
OutlookUserName = currentUser.Name;
if (currentUser.Type == "EX")
{
CurrentUserEmail = currentUser.SMTPAddress;
}
else
{
CurrentUserEmail = currentUser.Address;
}
if (string.IsNullOrWhiteSpace(CurrentUserEmail))
{
CurrentUserEmail = currentUser.Address;
//currentUserEmail = CCGlobals.OLCurrentUserEmail;
}
if (currentUser != null) Marshal.ReleaseComObject(currentUser);
}
//Occassionally we force Garbage Collection
GC.Collect();
return true;
}
catch (System.Exception ex)
{
Debug.DebugMessage(2,$"Error in InitialiseRedemption (RedemptionCode) {ex.Message}");
}
return false;
}
同步如下
private static void DoSync(RedemptionCode redemptionCode)
{
List<string> updatedAppointments;
List<string[]> plannedItemList = new List<string[]>();
List<string[]> pendingItemList = new List<string[]>();
try
{
pendingItemList = GetPendindItemList();
plannedItemList = GetplannedItemList();
var combinationOfPendingAndplannedItems = GetPendingAndplannedListWrapper(pendingItemList,plannedItemList);
if (combinationOfPendingAndplannedItems != null && Globals.UserSettings.SyncSettings == (int)SyncType.calendarandTasks)
{
updatedAppointments = redemptionCode.UpdateOrDeleteExistingAppointments(combinationOfPendingAndplannedItem
}
}
}
catch (Exception ex)
{
Debug.DebugMessage(2,$"Error in Helper (DoSync) {ex.Message}");
}
}
internal List<string> UpdateOrDeleteExistingAppointments(List<ItemWrapper> combinationOfPendingAndplannedItems)
{
RDOFolder calendarFolder = null;
RDOItems calendarFolderItems = null;
RDOAppointmentItem appointment = null;
List<string> updatedAppointmentIdList = new List<string>();
List<string> appointmentIdListTobedeleted = new List<string>();
try
{
Debug.DebugMessage(3,"RedemptionCode : UpdateOrDeleteExistingAppointments() : Started");
calendarFolder = rSession.GetDefaultFolder(rdoDefaultFolders.olFolderCalendar);
if (calendarFolder != null)
{
calendarFolderItems = calendarFolder.Items;
if (calendarFolderItems != null)
{
for (int i = 1; i <= calendarFolderItems.Count; i++)
{
try
{
appointment = calendarFolderItems[i] as RDOAppointmentItem;
if (appointment != null)
{
var updateOrDeleteItems = UpdateOrReturnAppointemntsTobedeleted(ref appointment,combinationOfPendingAndplannedItems);
if (updateOrDeleteItems != null)
{
if (!string.IsNullOrWhiteSpace(updateOrDeleteItems.updatedItem))
{
updatedAppointmentIdList.Add(updateOrDeleteItems.updatedItem);
}
if (!string.IsNullOrWhiteSpace(updateOrDeleteItems.itemTobedeleted))
{
appointmentIdListTobedeleted.Add(updateOrDeleteItems.itemTobedeleted);
}
}
}
}
catch (Exception ex)
{
Debug.DebugMessage(2,$"Error in UpdateOrDeleteExistingAppointments (RedemptionCode)[For loop] {ex.Message}");
}
finally
{
if (appointment != null) Marshal.ReleaseComObject(appointment);
}
}
if (appointmentIdListTobedeleted != null && appointmentIdListTobedeleted.Count > 0)
{
DeleteMarkedAsCompletedAppointments(appointmentIdListTobedeleted);
}
}
}
Debug.DebugMessage(3,"RedemptionCode : UpdateOrDeleteExistingAppointments() : Completed");
return updatedAppointmentIdList;
}
catch (Exception ex)
{
Debug.DebugMessage(2,$"Error in UpdateOrDeleteExistingAppointments (RedemptionCode) {ex.Message}"); ;
return updatedAppointmentIdList;
}
finally
{
if (calendarFolderItems != null) Marshal.ReleaseComObject(calendarFolderItems);
if (calendarFolder != null) Marshal.ReleaseComObject(calendarFolder);
}
}
calendarFolder = rSession.GetDefaultFolder(rdoDefaultFolders.olFolderCalendar);
谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。