ActiveX User Control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dan

    ActiveX User Control

    Good Day All,

    I am having a problem in Visual Studio 2005 Beta 2. I am hoping someone
    might have an idea as to what is going on.

    I have an ActiveX User Control written using Visual Basic 6.0. Using Visual
    Studio 2005 I add the control to my Toolbox. I then drag the control onto a
    Panel control that is on a form. That is it. I don't write any code at all.
    When I run the program I get the following error:

    Cross-thread operation not valid: Control 'Panel1' accessed from a thread
    other than the thread it was created on


    The error appears right away and the form is never rendered to the screen.
    This happens if I try creating the Windows Form project in either VB.NET or
    C#.NET. However, if I follow the same steps in Visual C++.NET then the
    application will start and the form will show up like it is suppose to.

    I am confused by this. There has to be something going on under the hood
    that allows C++ to handle this when the other languages don't. If anyone has
    any ideas I would appreciate it.


    Thanks!

    Dan DeLuca

  • Willy Denoyette [MVP]

    #2
    Re: ActiveX User Control


    "Dan" <Dan@discussion s.microsoft.com > wrote in message
    news:CFFBE96B-06EC-43FE-A70D-AECEC702019A@mi crosoft.com...[color=blue]
    > Good Day All,
    >
    > I am having a problem in Visual Studio 2005 Beta 2. I am hoping someone
    > might have an idea as to what is going on.
    >
    > I have an ActiveX User Control written using Visual Basic 6.0. Using
    > Visual
    > Studio 2005 I add the control to my Toolbox. I then drag the control onto
    > a
    > Panel control that is on a form. That is it. I don't write any code at
    > all.
    > When I run the program I get the following error:
    >
    > Cross-thread operation not valid: Control 'Panel1' accessed from a thread
    > other than the thread it was created on
    >
    >
    > The error appears right away and the form is never rendered to the screen.
    > This happens if I try creating the Windows Form project in either VB.NET
    > or
    > C#.NET. However, if I follow the same steps in Visual C++.NET then the
    > application will start and the form will show up like it is suppose to.
    >
    > I am confused by this. There has to be something going on under the hood
    > that allows C++ to handle this when the other languages don't. If anyone
    > has
    > any ideas I would appreciate it.
    >
    >
    > Thanks!
    >
    > Dan DeLuca
    >[/color]

    Your code or the ActiveX control's code are accessing the UI from another
    thread than the UI thread. This is not allowed in windows, v2.0 has a debug
    probe that signals this (wrong) behavior when running in the debugger.
    Make sure:
    1. you don't create the ActiveX control object from another thread (guess
    you don't)
    2. you have your Main attributed with [STAThread].

    Willy.


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: ActiveX User Control

      Dan,

      Just curious, on the entry point to your application, do you have the
      STAThread attribute on the Main method? You will need this for Windows
      Forms applications, especially ones using ActiveX controls. If it is not
      there, then the program is in the multi-threaded apartment, which is a no-no
      for ActiveX controls.

      Hope this helps.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "Dan" <Dan@discussion s.microsoft.com > wrote in message
      news:CFFBE96B-06EC-43FE-A70D-AECEC702019A@mi crosoft.com...[color=blue]
      > Good Day All,
      >
      > I am having a problem in Visual Studio 2005 Beta 2. I am hoping someone
      > might have an idea as to what is going on.
      >
      > I have an ActiveX User Control written using Visual Basic 6.0. Using
      > Visual
      > Studio 2005 I add the control to my Toolbox. I then drag the control onto
      > a
      > Panel control that is on a form. That is it. I don't write any code at
      > all.
      > When I run the program I get the following error:
      >
      > Cross-thread operation not valid: Control 'Panel1' accessed from a thread
      > other than the thread it was created on
      >
      >
      > The error appears right away and the form is never rendered to the screen.
      > This happens if I try creating the Windows Form project in either VB.NET
      > or
      > C#.NET. However, if I follow the same steps in Visual C++.NET then the
      > application will start and the form will show up like it is suppose to.
      >
      > I am confused by this. There has to be something going on under the hood
      > that allows C++ to handle this when the other languages don't. If anyone
      > has
      > any ideas I would appreciate it.
      >
      >
      > Thanks!
      >
      > Dan DeLuca
      >[/color]


      Comment

      • Dan

        #4
        Re: ActiveX User Control

        Willy and Nicholas,

        Thanks for responding to my post so quickly. I appreciate it.

        I looked at the C#, VB.NET, and C++ versions of the project. All three had
        the STAThread attribute applied to the main method. Despite this I still get
        the error on the C# and VB.NET versions but not on the C++ version.

        Do you have any other ideas as to how I can deal with this or what can be
        causing it? I did check the Active X control and made sure that it was also
        set to Single Thread.

        Thanks!

        Dan DeLuca

        Comment

        • Willy Denoyette [MVP]

          #5
          Re: ActiveX User Control


          "Dan" <Dan@discussion s.microsoft.com > wrote in message
          news:63FE97ED-0D8B-48BE-88A8-9EA222C92E39@mi crosoft.com...[color=blue]
          > Willy and Nicholas,
          >
          > Thanks for responding to my post so quickly. I appreciate it.
          >
          > I looked at the C#, VB.NET, and C++ versions of the project. All three had
          > the STAThread attribute applied to the main method. Despite this I still
          > get
          > the error on the C# and VB.NET versions but not on the C++ version.
          >
          > Do you have any other ideas as to how I can deal with this or what can be
          > causing it? I did check the Active X control and made sure that it was
          > also
          > set to Single Thread.
          >
          > Thanks!
          >
          > Dan DeLuca[/color]

          What do you mean with "single thread", you really don't mean ThreadingModel
          = "Single" in the registry.
          If that's true you have to rebuild the control as "Apartment" threaded, and
          you should never ever build Single threaded controls to be used in 32 bit
          windows applications. These COM objects live on a OLE created/managed thread
          and not the Main UI thread, that would explain the cross-thread failure
          message.

          Willy.
          PS. Is the C++ application a managed application? Guess not, so the probe
          isn't used.





          Comment

          • Dan

            #6
            Re: ActiveX User Control

            Willy,

            If you lived in Omaha I would buy you a beer. That was the problem. The
            ActiveX control was set to Single and not Apartment. As soon as I changed
            that it worked just fine.

            Thank you so much for taking the time to help me through this issue. You
            have saved me countless hours of head scratching and I really appreciate it.

            Dan

            Comment

            Working...