ActiveX User Control

G

Guest

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
 
W

Willy Denoyette [MVP]

Dan said:
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

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
G

Guest

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
 
W

Willy Denoyette [MVP]

Dan said:
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

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.
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top