472,127 Members | 1,847 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

ActiveX User Control

Dan
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

Nov 17 '05 #1
5 2513

"Dan" <Da*@discussions.microsoft.com> wrote in message
news:CF**********************************@microsof t.com...
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.
Nov 17 '05 #2
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]
- mv*@spam.guard.caspershouse.com

"Dan" <Da*@discussions.microsoft.com> wrote in message
news:CF**********************************@microsof t.com...
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

Nov 17 '05 #3
Dan
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
Nov 17 '05 #4

"Dan" <Da*@discussions.microsoft.com> wrote in message
news:63**********************************@microsof t.com...
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.

Nov 17 '05 #5
Dan
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
Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Fie Fie Niles | last post: by
1 post views Thread by Sreejumon [MVP] | last post: by
5 posts views Thread by Andrew | last post: by
1 post views Thread by Anand Kale | last post: by
3 posts views Thread by Jeffery Franzen | last post: by
2 posts views Thread by Shawn | last post: by
7 posts views Thread by Jarod_24 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.