sleeviin
sleeviin

Reputation: 13

C# Outlook Addin The object invoked has disconnected from its clients

I'm doing a plugin for outlook, but when I try to get the namespaces for calendars, I get this error: https://i.sstatic.net/ZV6eN.png

So, in some computers not happen, but when another person try to install this error its showed, my code that break the app is this:

private async void AddinModule_AddinInitialize(object sender, EventArgs e)
{        
        try
        {
            var app = this.OutlookApp;
            Outlook.NameSpace nameSpace = app.GetNamespace("MAPI");
            foreach (Outlook.Store store in nameSpace.Stores)
            {
                if (store.IsCachedExchange)
                {
                    calendarFolders.Add(store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar));
                }
            }

            await updateAppItemList();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error CODE 0x01", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
}


        private Task updateAppItemList()
        {
            appItems = new List<Outlook.AppointmentItem>();
            foreach (Outlook.MAPIFolder folder in calendarFolders)
            {
                Outlook.Items items = folder.Items;
                var parent = items.Parent as Outlook.MAPIFolder;
                if (!calendarItems.Any(x => (x.Parent as Outlook.MAPIFolder).StoreID == folder.StoreID))
                {
                    items.ItemAdd += this.ItemAdd;
                    items.ItemChange += this.ItemChange;

                    calendarItems.Add(items);
                }

                foreach (var item in items)
                {
                    Outlook.AppointmentItem appItem = item as Outlook.AppointmentItem;
                    if (appItem != null)
                    {
                        appItem.BeforeDelete += this.ItemRemove;
                        appItems.Add(appItem);
                    }
                }
            }
            return Task.FromResult(0);
        }

Upvotes: 1

Views: 467

Answers (2)

Eugene Astafiev
Eugene Astafiev

Reputation: 49395

The AddinInitialize event (from ADX) is not the right place for such calls. For example, if multiple profiles are configured in Outlook and the dialog for chosing the profile is shown, the event can be fired before the user chooses a particular profile. So, the namespace is not initialized yet. That's why I'd suggest using the AddinStartupComplete event handler instead.

As for multithreading, Office applications use the single threaded apartment model (STA) and all calls made from another threads are marshalled to the main one (Outlook 2013 and later). Moreover, previous Outlook versions may fire exceptions in such cases. Use the main thread for dealing with the Outlook object model.

As a workaround, you can use the low-level API (Extended MAPI) on which Outlook and other third-party components are based. In that case you can run your low-level code on another thread.

Finally, I've noticed that you don't release underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article.

Upvotes: 0

Sanjay Karia
Sanjay Karia

Reputation: 318

The Outlook Object Model(OOM) is not suited for multi-threading, specifically accessing the OOM in multiple threads.

http://blogs.msdn.com/b/pcreehan/archive/2008/03/13/outlook-crashes-when-using-outlook-object-model-in-multiple-threads.aspx

Google search for "outlook multi thread issues" and check out.

Upvotes: 1

Related Questions