MTA Thread VSTA V2.0

Last post 05-08-2008, 12:43 AM by Alec Cozens. 4 replies.
Sort Posts: Previous Next
  •  05-06-2008, 7:48 AM 1086

    MTA Thread VSTA V2.0

    Hi,

    I'm writing a WPF application that calls code in an AddIn which is effectively

        public Splash GetAboutDialog(){
          return new Splash();
        }

      class Splash: Window
      { .... }

    I use MethodInvoke to call via RunCommand( "GetAboutDialog" )

        internal bool RunCommand(string macroName)
        {
          IExtendedEntryPoint entryPoint = this.macroAddIn as IExtendedEntryPoint;
          if (entryPoint == null)
            return false;

          object obj = entryPoint.GetEntryPointObject();
          Type t = obj.GetType();
          try
          {
            t.InvokeMember(macroName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, obj, null);
            return true;
          }
          ....
        }

    Now this code works perfectly when not in Debug mode, but once in the debugger, it will fail with the following:

    The calling thread must be STA, because many UI components require this.

    Delving further, the thread calling GetAboutDialog is indeed MTA when in the debugger, but presumably not when the code is called normally. I presume that AddInProcess that is hosting the addin in the debugger is starting the thread the MTA apartment model. What can I do to resolve this?

    Many thanks,

    Alec

  •  05-06-2008, 3:16 PM 1087 in reply to 1086

    Re: MTA Thread VSTA V2.0

    Hi Alec,

    Let me do some testing and get some more information from Microsoft on this problem.

    Best regards,
    Rebecca

     

  •  05-07-2008, 1:23 AM 1088 in reply to 1087

    Re: MTA Thread VSTA V2.0

    This little macro demonstrates exactly what you suspect...

    public void Macro3()
    {
    MessageBox.Show("Threading State: " + System.Threading.Thread
    .CurrentThread.GetApartmentState().ToString());
    }

    I'm still digging for answers on it.

  •  05-07-2008, 2:18 PM 1089 in reply to 1088

    Re: MTA Thread VSTA V2.0

    FYI - we're working with Microsoft on this. I'll update this thread as soon as I get more information.
  •  05-08-2008, 12:43 AM 1091 in reply to 1089

    Re: MTA Thread VSTA V2.0

    Thanks for keeping me informed Rebecca.

    Ideally, I suppose that the thread model should be configurable in some way.

    Regards,

    Alec

View as RSS news feed in XML