在Outlook加载项中获取跨线程操作异常

时间:2013-02-13 09:53:38

标签: c# asynchronous outlook-addin

开发Outlook-2010插件。

Main addin类正在启动像这样的异步任务并声明 一个静态事件,可以从其他形式中获取:

        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()
       {

            try
            {           
                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);


                for (int i = 1; i <= ArchivarFolder.Items.Count; i++)
                {
                    try
                    {

                      item = (Microsoft.Office.Interop.Outlook.MailItem)ArchivarFolder.Items[i];
                      ProcesadosArchivado += 1;
                      OnFileArchivedDelegate myEvent = OnFileArchivedEvent;
                      myEvent.Invoke(ProcesadosArchivado, item.Subject);                          


                    //Messate iteration
                    }
                    catch (Exception ex)
                    {

                        throw;
                    }

                }

            }

            //Catch folder iteration
            catch (Exception ex)
            {

                throw;
            }

        }

然后我从其他表单中接受静态事件并尝试使用更新某些控件 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;

        }


    }

我得到Cross-Head无效的操作异常 到达线时:

* prop.SetValue(control,Convert.ChangeType(value,prop.PropertyType),null); *

它正在通过invoreRequired并调用委托,所以我真的没有 得到为什么我得到这个例外。

2 个答案:

答案 0 :(得分:0)

好的,经过一些研究我改变了这个:

del.Invoke(lblAsunto, "Text", "Asunto: " + NameArchived);

进入这个:

this.Invoke(del, new object[] { lblAsunto, "Text", "Asunto: " + NameArchived });

它现在正在运作。

答案 1 :(得分:0)

您不应该使用辅助线程中的Outlook对象模型。它必然会产生问题。如果从与返回它的线程不同的线程访问OOM对象,Outlook 2013将立即引发异常。在大多数情况下,Outlook迁移的较旧版本会工作,但在最不幸的时刻会失败。
上面的代码在辅助线程上创建了Outlook.Application对象的新实例。你可以这样做,但是所有的调用都将被编组到主要的Outlook线程中,因此使用辅助线程对你没有多大帮助。
唯一的解决方法是使用扩展MAPi(C ++或Delphi)或使用MAPI的封装器(例如Redemption)。