我写了一个同步我的Outlook日历的程序。到目前为止,程序运行良好。但是如果Outlook崩溃或重新启动,我的程序确实需要触发" ThisAddin_Startup()"再次链接到新的outlook实例。我试过通过监视正在运行的进程来解决这个问题。但后来它再也没有用了。如果我打电话给" Process.GetProcessesByName"在循环中没有任何工作它的评论然后它确实有效。
有谁知道为什么?
bool monitoring = false;
while (true)
{
if (!monitoring) //Start monitoring or restart monitoring
{
Debug.WriteLine("start");
ThisAddIn_Startup();
monitoring = true;
}
Process[] outlook;
outlook = Process.GetProcessesByName("blabla");
Thread.Sleep(1000);
}
private void ThisAddIn_Startup()
{
_OutlookApplication = new Outlook.Application();
//Link Inbodx Events
_olFolderInbox.Items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(Item_Calendar_Change);
//Link Calendar Events
_olFolderCalendar.Items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(Item_Calendar_Add);
_olFolderCalendar.Items.ItemChange += new Outlook.ItemsEvents_ItemChangeEventHandler(Item_Calendar_Change);
_olFolderCalendar.Items.ItemRemove += new Outlook.ItemsEvents_ItemRemoveEventHandler(Item_Calendar_Deleted);
}
编辑: 简化孔代码仍然无效:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Diagnostics;
using System.Threading;
using System.ComponentModel;
namespace OAddIn
{
public partial class Form1 : Form
{
Outlook.Application _OutlookApplication;
Outlook.MAPIFolder _olFolderInbox;
Outlook.MAPIFolder _olFolderCalendar;
Outlook.MAPIFolder _olFolderCalendar_AddOnCalendar;
Outlook.MAPIFolder _olFolderDeletedItems;
public Form1()
{
InitializeComponent();
Thread osThread = new Thread(ThisAddIn_Startup);
osThread.Start();
Thread osThread2 = new Thread(CheckIfProcessIsRunning);
osThread2.Start();
}
private void ThisAddIn_Startup()
{
_OutlookApplication = new Outlook.Application();
InitCalendars();
InitMailBox();
Debug.WriteLine("Start Now Monitoring");
//Link Inbodx Events
_olFolderInbox.Items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(Item_Calendar_Change);
//Link Calendar Events
_olFolderCalendar.Items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(Item_Calendar_Add);
_olFolderCalendar.Items.ItemChange += new Outlook.ItemsEvents_ItemChangeEventHandler(Item_Calendar_Change);
_olFolderCalendar.Items.ItemRemove += new Outlook.ItemsEvents_ItemRemoveEventHandler(Item_Calendar_Deleted);
}
void CheckIfProcessIsRunning()
{
while (true)
{
int returnValue = 0;
Process[] o = Process.GetProcessesByName("OUTLOOK");
Debug.WriteLine("FSSFSSGSFSFSFSFSFSF: " + returnValue);
Thread.Sleep(3000);
}
}
private void InitCalendars()
{
Outlook.MAPIFolder calendars = (Outlook.MAPIFolder)_OutlookApplication.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
try
{
_olFolderCalendar_AddOnCalendar = calendars.Folders["AddOnCalendar"]; //AddOn calendar
}
catch
{
_olFolderCalendar_AddOnCalendar = calendars.Folders.Add("AddOnCalendar");
}
_olFolderCalendar = calendars; //Standard calendar
}
private void InitMailBox()
{
_olFolderInbox = (Outlook.MAPIFolder)_OutlookApplication.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
_olFolderDeletedItems = (Outlook.MAPIFolder)_OutlookApplication.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDeletedItems);
}
void Item_Calendar_Add(object item)
{
Debug.WriteLine("");
Debug.WriteLine("------ Item_Calendar_Add() ----------------------------------------------");
PrintImportantInfo(item);
}
public void Item_Calendar_Change(object item)
{
Debug.WriteLine("");
Debug.WriteLine("------ Item_Calendar_Change() ----------------------------------------------");
PrintImportantInfo(item);
}
void Item_Calendar_Deleted()
{
Debug.WriteLine("");
Debug.WriteLine("------ Item_Calendar_Deleted() ---------------------------------------------");
object item = _olFolderDeletedItems.Items.GetLast();
PrintImportantInfo(item);
}
void PrintImportantInfo(object item)
{
try
{
Outlook.AppointmentItem appointment = item as Outlook.AppointmentItem;
if (appointment != null)
{
Debug.WriteLine("APPOINTMENTINFO_________________________________________________________");
Debug.WriteLine(" A_Subject: " + appointment.Subject.ToString());
Debug.WriteLine(" A_MeetingStatus: " + appointment.MeetingStatus.ToString());
Debug.WriteLine(" A_ResponseStatus: " + appointment.ResponseStatus.ToString());
Debug.WriteLine(" A_GlobalAppointmentID: " + appointment.GlobalAppointmentID.ToString());
Debug.WriteLine(" A_Time: " + appointment.Start.ToString() + " - " + appointment.End.ToString());
Debug.WriteLine(" A_Organizer: " + appointment.GetOrganizer().Address.ToString());
Debug.WriteLine(" A_Organizer: " + appointment.Organizer);
if (appointment.Location != null)
Debug.WriteLine(" A_Location: " + appointment.Location.ToString());
}
Outlook.MeetingItem meeting = item as Outlook.MeetingItem;
if (meeting != null)
{
Debug.WriteLine("MEETINGINFO_____________________________________________________________");
Debug.WriteLine(" M_Subject: " + meeting.Subject.ToString());
Debug.WriteLine(" M_MessageClass: " + meeting.MessageClass.ToString());
Debug.WriteLine(" M_SenderEmailAddress: " + meeting.SenderEmailAddress.ToString());
appointment = meeting.GetAssociatedAppointment(false);
if (appointment != null)
{
Debug.WriteLine("APPOINTMENTINFO_________________________________________________________");
appointment.Save();
Debug.WriteLine(" A_Subject: " + appointment.Subject.ToString());
Debug.WriteLine(" A_MeetingStatus: " + appointment.MeetingStatus.ToString());
Debug.WriteLine(" A_ResponseStatus: " + appointment.ResponseStatus.ToString());
Debug.WriteLine(" A_GlobalAppointmentID: " + appointment.GlobalAppointmentID.ToString());
Debug.WriteLine(" A_Time: " + appointment.Start.ToString() + " - " + appointment.End.ToString());
Debug.WriteLine(" A_Organizer: " + appointment.GetOrganizer().Address.ToString());
Debug.WriteLine(" A_Organizer: " + appointment.Organizer.ToString());
if (appointment.Location != null)
Debug.WriteLine(" A_Location: " + appointment.Location.ToString());
}
}
}
catch { }
}
}
}
答案 0 :(得分:1)
您是否尝试过在单独的线程中执行每个操作?
bool monitoring = false;
while (true)
{
if (!monitoring) //Start monitoring or restart monitoring
{
Debug.WriteLine("start");
ThisAddIn_Startup();
monitoring = true;
}
Process[] outlook;
new Thread(() => {
outlook = Process.GetProcessesByName("blabla");
}.Start();
Thread.Sleep(1000);
}
private void ThisAddIn_Startup()
{
_OutlookApplication = new Outlook.Application();
//Link Inbodx Events
_olFolderInbox.Items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(Item_Calendar_Change);
//Link Calendar Events
_olFolderCalendar.Items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(Item_Calendar_Add);
_olFolderCalendar.Items.ItemChange += new Outlook.ItemsEvents_ItemChangeEventHandler(Item_Calendar_Change);
_olFolderCalendar.Items.ItemRemove += new Outlook.ItemsEvents_ItemRemoveEventHandler(Item_Calendar_Deleted);
}