none
Invoke in Outlook VSTO ThisAddIn_Startup? RRS feed

  • Question

  • (using vb.net)

    In my ThisAddin_Startup, I use this...

    System.Threading.Tasks.Task.Run(Sub() MySub())

    In MySub, I need to show a form, but how can I test for InvokeRequired?

    When I try to show the form, Outlook freezes.

    Thanks

    Mojo

    Wednesday, May 8, 2013 5:50 PM

Answers

  • You can create some control in ThisAddin_Startup and use that control from MySub() to check if invoke is required.

    Example (C#):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    using System.Windows.Forms;
    
    namespace OutlookAddIn1
    {
        public partial class ThisAddIn
        {
            private UserControl syncControl;
    
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                syncControl = new UserControl();
                IntPtr handle = syncControl.Handle;
    
                System.Threading.Thread thread = new System.Threading.Thread(BackgroundThreadMethod);
                thread.IsBackground = true;
                thread.Start();
            }
    
            private void BackgroundThreadMethod()
            {
                System.Threading.Thread.Sleep(5000);
    
                if (syncControl.InvokeRequired)
                {
                    syncControl.Invoke(new MethodInvoker(delegate
                        {
                            UiThreadMethod();
                        }));
                }
                else
                {
                    UiThreadMethod();
                }
            }
    
            private void UiThreadMethod()
            {
                //...
            }
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            #region VSTO generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
    
            #endregion
        }
    }
    

    • Marked as answer by M O J O Friday, May 17, 2013 4:42 PM
    Thursday, May 9, 2013 11:47 PM

All replies

  • You can create some control in ThisAddin_Startup and use that control from MySub() to check if invoke is required.

    Example (C#):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    using System.Windows.Forms;
    
    namespace OutlookAddIn1
    {
        public partial class ThisAddIn
        {
            private UserControl syncControl;
    
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                syncControl = new UserControl();
                IntPtr handle = syncControl.Handle;
    
                System.Threading.Thread thread = new System.Threading.Thread(BackgroundThreadMethod);
                thread.IsBackground = true;
                thread.Start();
            }
    
            private void BackgroundThreadMethod()
            {
                System.Threading.Thread.Sleep(5000);
    
                if (syncControl.InvokeRequired)
                {
                    syncControl.Invoke(new MethodInvoker(delegate
                        {
                            UiThreadMethod();
                        }));
                }
                else
                {
                    UiThreadMethod();
                }
            }
    
            private void UiThreadMethod()
            {
                //...
            }
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            #region VSTO generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
    
            #endregion
        }
    }
    

    • Marked as answer by M O J O Friday, May 17, 2013 4:42 PM
    Thursday, May 9, 2013 11:47 PM
  • I usually set up a hidden Windows.Form object in the startup code. That will run in the same thread context as the addin and Outlook. In the form I have callls to the methods I need and can use the form's InvokeRequired property to see if I need to invoke a delegate call to the method.

    That also works nicely if I need to call into the Outlook object model from a background thread, which is not supported unless the call runs in the same context as Outlook and the addin.


    Ken Slovak MVP - Outlook

    Friday, May 10, 2013 2:32 PM
    Moderator