i wrote program syncs outlook calendar. far program works fine. if outlook crashes or gets restarted program need fire "thisaddin_startup()" again link new outlook instance. tried solve monitoring running processes. didn't work more. if call "process.getprocessesbyname" in loop nothing works commented work.
does know why?
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); }
edit: simplified hole code still nothing works:
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 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 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 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 { } } } }
have tried doing each action within separate thread?
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); }
Comments
Post a Comment