c# - COM object that has been separated from its underlying RCW cannot be used with Outlook -
i have application loads email-addresses list , uses these autocompletion in textbox. if close application while these addresses still being retrieved invalidcomobjectexception unhandled user code error.
the error specified on piece of code:
outlook.namespace ons = ((microsoft.office.interop.outlook.application)outlookobj).getnamespace("mapi");
this entire auto-completion class:
class autocomplete { list<string> emaillist = new list<string>(); outlook.application outlookobj = new outlook.application(); string[] names; public list<string> getemails() { outlook.addresslists addresslists = outlookobj.session.addresslists; outlook.addresslist gal = addresslists["all users"]; outlook.addressentries addrentries = gal.addressentries; names = new string[addrentries.count]; (int = 1; < addrentries.count; i++) { outlook.addressentry exchdlmember = addrentries[i]; outlook.namespace ons = ((microsoft.office.interop.outlook.application)outlookobj).getnamespace("mapi"); outlook.recipient recip = ons.createrecipient(exchdlmember.name); recip.resolve(); if (recip.address != null && recip.addressentry != null) { if (recip.addressentry.getexchangeuser().primarysmtpaddress != null) { names[i] = recip.addressentry.getexchangeuser().primarysmtpaddress; } } } foreach (string combo in names) { if (combo != null) { emaillist.add(combo); } else { } } return emaillist; } }
the backgroundworker methods in form:
void form1_shown(object sender, eventargs e) { bgw.runworkerasync(); bgw.workersupportscancellation = true; } void bgw_dowork(object sender, doworkeventargs e) { bgw.workersupportscancellation = true; if(textbox1.invokerequired) { emails = autocomplete.getemails(); collection.addrange(emails.toarray()); textbox1.invoke(new methodinvoker(delegate { textbox1.autocompletecustomsource = collection; })); } (int = 100; <= 100; i++) { bgw.reportprogress(i); if (i == 100) { label2.invoke(new methodinvoker(delegate { label2.text = "global address list loaded!"; })); label2.invoke(new methodinvoker(delegate { label2.backcolor = system.drawing.color.lightgreen; })); } } }
and upon closing form:
private void form1_formclosing(object sender, formclosingeventargs e) { bgw.cancelasync(); }
anyone have idea preventing error?
hans passant: sure, environment.exit(). bam, gone.
adding enviroment.exit(0); form1_formclosing() hans said did trick gets closed when exiting application now.
Comments
Post a Comment