c# - Getting Cross-thread operation Exception in Outlook add-in -



c# - Getting Cross-thread operation Exception in Outlook add-in -

im developing outlook-2010 addin.

the main addin class launching asynchronous task , declaring static event suscribe other form:

int procesadosarchivado = 0; public delegate void onfilearchiveddelegate (int numfilesarchived, string namearchived); public static event onfilearchiveddelegate onfilearchivedevent = delegate { }; private void thisaddin_startup(object sender, system.eventargs e) { thread hiloarchivado = new thread(doarchivebackground); hiloarchivado.start(); } private void doarchivebackground() { seek { outlook.application app = null; outlook._namespace ns = null; outlook.mailitem item = null; //outlook.mapifolder inboxfolder = null; datetime mydatetime = datetime.now.addmonths(-3); app = new outlook.application(); ns = app.getnamespace("mapi"); ns.logon(null, null, false, false); outlook.stores store; outlook.mapifolder rootfolder = null; store = application.session.stores; foreach (outlook.store storeclass in store.session.stores) { rootfolder = storeclass.getrootfolder(); } outlook.mapifolder folder = rootfolder.folders["archivar"]; outlook.mapifolder archivarfolder = ns.getdefaultfolder(microsoft.office.interop.outlook.oldefaultfolders.olfolderinbox); // inboxfolder = ns.getdefaultfolder(microsoft.office.interop.outlook.oldefaultfolders.olfolderinbox); (int = 1; <= archivarfolder.items.count; i++) { seek { item = (microsoft.office.interop.outlook.mailitem)archivarfolder.items[i]; procesadosarchivado += 1; onfilearchiveddelegate myevent = onfilearchivedevent; myevent.invoke(procesadosarchivado, item.subject); //messate iteration } grab (exception ex) { throw; } } } //catch folder iteration grab (exception ex) { throw; } }

then suscribe static event other form , seek update controls using invokerequired , delegates:

public partial class archiveform : form { private delegate void updatecontroldelegate (control control,string property,string value); public archiveform() { initializecomponent(); this.load += new eventhandler(archiveform_load); } void archiveform_load(object sender, eventargs e) { thisaddin.onfilearchivedevent += new thisaddin.onfilearchiveddelegate(thisaddin_onfilearchivedevent); } void thisaddin_onfilearchivedevent(int numfilesarchived, string namearchived) { updatecontrols(numfilesarchived,namearchived); } void updatecontrol(control control,string property,string value) { propertyinfo prop = control.gettype().getproperty("text"); prop.setvalue(control, convert.changetype(value, prop.propertytype), null); } private void updatecontrols(int numfilesarchived, string namearchived) { if (lblarchivado.invokerequired) { updatecontroldelegate del = new updatecontroldelegate(updatecontrol); //del.begininvoke(lblarchivado, "text", "archivados: " + numfilesarchived.tostring(), null, null); del.invoke(lblarchivado, "text", "archivados: " + numfilesarchived.tostring()); } else this.lblarchivado.text = "archivados: " + numfilesarchived.tostring(); if (lblasunto.invokerequired) { updatecontroldelegate del = new updatecontroldelegate(updatecontrol); //del.begininvoke(lblasunto, "text", "asunto: " + namearchived, null, null); del.invoke(lblasunto, "text", "asunto: " + namearchived); } else this.lblasunto.text = "asunto: " + namearchived; } }

im getting cross-head invalid operation exception when reaching line:

*prop.setvalue(control,convert.changetype(value, prop.propertytype), null);*

it getting through invorerequired , invoking delegate dont why getting exception.

ok, after doing research changed this:

del.invoke(lblasunto, "text", "asunto: " + namearchived);

into this:

this.invoke(del, new object[] { lblasunto, "text", "asunto: " + namearchived });

and working now.

c# asynchronous outlook-addin

Comments

Popular posts from this blog

javascript - mongodb won't find my schema method in nested container -

How do you set up a perforce server to work over the internet? -

ios - Lagging ScrollView with UIWebview inside -