By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,282 Members | 1,549 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,282 IT Pros & Developers. It's quick & easy.

ActiveX User Control

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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.