473,756 Members | 7,019 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

exception

need help

i Have this error in my project:
"An unhandled exception of type 'System.Threadi ng.ThreadStateE xception'
occurred in system.windows. forms.dll
Additional information: Could not instantiate ActiveX control
'648a5600-2c6e-101b-82b6-000000000014' because the current thread is not in
a single-threaded apartment."

on this row :
this.axMSComm1 = new AxMSCommLib.AxM SComm();

, but if i create new project with one form and put this mscomm32.ocx
it is work thery good and i can get data from com1.
what is the problem ?

Thanks
Nov 16 '05
18 5714
Hey, come on, I didn't make the technology!

When the ApartmentState( s) differ between Com and .Net components and
the communication has to happen across processes then marshalling has to
be adopted, right? In the case of different apartment states,
marshalling is taken care of by the underlying platform to some extent -
the extent being that if the apartment states are similar then the CLR's
Interop Marshaler can take care of most of the marshalling unless there
are non-blittable types involved in which case you need to use the
MarshalAs attribute to solve the purpose. In case of differing apartment
state(s), the COM Interop marshaler takes over and hence, there was an
error as the COM marhaller does not know .Net types.

But, since the Windows Application (in V.Net) inserts a Main method with
a [STAThread] attribute the apartment states are equalized and hence, no
error for the OP.

I hope this answer satisfies your incredulity!

with regards,
J.V.Ravichandra n
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandra n+J.V.&cob=aspn etpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID= P3966388&BN=999 &PN=2
- Or, just search on "J.V.Ravichandr an"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #11
OK - I thought I better check this to ensure they didn't break it since 1.0 alpha (I tested it out then as I was teaching Essential COM in those days).

I just wrote an MTA COM object which took a BSTR as a parameter ( non-blittable) and called it from an application with the STAThread attribute on the Main. So both the Interop and the COM marshaller were involved. Works fine.

I've seen a couple of posts over the last few months that seemed to suggest that performing cross-apartment calls from managed code to unmamaged code wasn't supported. I thought I was maybe just misreading what they were saying and so let it pass. But there is, it seems, some currency in the idea that it doesn't work

There is not just one marshalling layer in place. The RCW uses the interop layer to get into the COM type system and then the COM marsaller (in my case the universal marshaller) builds the COM marshalling info for cross apartment invocation. The object is invoked, then COM marshals the call back to the STA (in my example) and the RCW managed the transition back to the CLR type system.

The crucial reason why you want to use the same apartment type when invoking COM objects is to remove the need for the COM marshalling layer which adds a non-trivial overhead with "chatty" interfaces (ones which perform alot of calls having, say, lots of properties on them).

ActiveX controls have other issues as they are UI code. Its not specifically that they are STA based.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft. com/microsoft.publi c.dotnet.langua ges.csharp/<e5************ **@TK2MSFTNGP12 .phx.gbl>

Hey, come on, I didn't make the technology!

When the ApartmentState( s) differ between Com and .Net components and
the communication has to happen across processes then marshalling has to
be adopted, right? In the case of different apartment states,
marshalling is taken care of by the underlying platform to some extent -
the extent being that if the apartment states are similar then the CLR's
Interop Marshaler can take care of most of the marshalling unless there
are non-blittable types involved in which case you need to use the
MarshalAs attribute to solve the purpose. In case of differing apartment
state(s), the COM Interop marshaler takes over and hence, there was an
error as the COM marhaller does not know .Net types.

But, since the Windows Application (in V.Net) inserts a Main method with
a [STAThread] attribute the apartment states are equalized and hence, no
error for the OP.

I hope this answer satisfies your incredulity!
Nov 16 '05 #12
OK - I thought I better check this to ensure they didn't break it since 1.0 alpha (I tested it out then as I was teaching Essential COM in those days).

I just wrote an MTA COM object which took a BSTR as a parameter ( non-blittable) and called it from an application with the STAThread attribute on the Main. So both the Interop and the COM marshaller were involved. Works fine.

I've seen a couple of posts over the last few months that seemed to suggest that performing cross-apartment calls from managed code to unmamaged code wasn't supported. I thought I was maybe just misreading what they were saying and so let it pass. But there is, it seems, some currency in the idea that it doesn't work

There is not just one marshalling layer in place. The RCW uses the interop layer to get into the COM type system and then the COM marsaller (in my case the universal marshaller) builds the COM marshalling info for cross apartment invocation. The object is invoked, then COM marshals the call back to the STA (in my example) and the RCW managed the transition back to the CLR type system.

The crucial reason why you want to use the same apartment type when invoking COM objects is to remove the need for the COM marshalling layer which adds a non-trivial overhead with "chatty" interfaces (ones which perform alot of calls having, say, lots of properties on them).

ActiveX controls have other issues as they are UI code. Its not specifically that they are STA based.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft. com/microsoft.publi c.dotnet.langua ges.csharp/<e5************ **@TK2MSFTNGP12 .phx.gbl>

Hey, come on, I didn't make the technology!

When the ApartmentState( s) differ between Com and .Net components and
the communication has to happen across processes then marshalling has to
be adopted, right? In the case of different apartment states,
marshalling is taken care of by the underlying platform to some extent -
the extent being that if the apartment states are similar then the CLR's
Interop Marshaler can take care of most of the marshalling unless there
are non-blittable types involved in which case you need to use the
MarshalAs attribute to solve the purpose. In case of differing apartment
state(s), the COM Interop marshaler takes over and hence, there was an
error as the COM marhaller does not know .Net types.

But, since the Windows Application (in V.Net) inserts a Main method with
a [STAThread] attribute the apartment states are equalized and hence, no
error for the OP.

I hope this answer satisfies your incredulity!
Nov 16 '05 #13

"Richard Blewett [DevelopMentor]" <ri******@NOSPA Mdevelop.com> wrote in
message news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
OK - I thought I better check this to ensure they didn't break it since
1.0 alpha (I tested it out then as I was teaching Essential COM in those
days).

I just wrote an MTA COM object which took a BSTR as a parameter (
non-blittable) and called it from an application with the STAThread
attribute on the Main. So both the Interop and the COM marshaller were
involved. Works fine.


Richard,

I suppose that with MTA COM object you mean a "free-threaded" apartmentmodel
COM object, else if it's marked "Both" it will be created on the main STA
thread. Sure, I ain't telling you something new, just to be sure ;-)

Willy.

Nov 16 '05 #14

"Richard Blewett [DevelopMentor]" <ri******@NOSPA Mdevelop.com> wrote in
message news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
OK - I thought I better check this to ensure they didn't break it since
1.0 alpha (I tested it out then as I was teaching Essential COM in those
days).

I just wrote an MTA COM object which took a BSTR as a parameter (
non-blittable) and called it from an application with the STAThread
attribute on the Main. So both the Interop and the COM marshaller were
involved. Works fine.


Richard,

I suppose that with MTA COM object you mean a "free-threaded" apartmentmodel
COM object, else if it's marked "Both" it will be created on the main STA
thread. Sure, I ain't telling you something new, just to be sure ;-)

Willy.

Nov 16 '05 #15
Nope, ThreadingModel= free
Not ThreadingModel= both

My COM object was deinfitely running in the MTA and the .NET Main thread entered an STA. Want me to post the code?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft. com/microsoft.publi c.dotnet.langua ges.csharp/<er************ **@TK2MSFTNGP14 .phx.gbl>
"Richard Blewett [DevelopMentor]" <ri******@NOSPA Mdevelop.com> wrote in
message news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
OK - I thought I better check this to ensure they didn't break it since
1.0 alpha (I tested it out then as I was teaching Essential COM in those
days).

I just wrote an MTA COM object which took a BSTR as a parameter (
non-blittable) and called it from an application with the STAThread
attribute on the Main. So both the Interop and the COM marshaller were
involved. Works fine.


Richard,

I suppose that with MTA COM object you mean a "free-threaded" apartmentmodel
COM object, else if it's marked "Both" it will be created on the main STA
thread. Sure, I ain't telling you something new, just to be sure ;-)

Willy.


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.782 / Virus Database: 528 - Release Date: 22/10/2004

[microsoft.publi c.dotnet.langua ges.csharp]
Nov 16 '05 #16
Nope, ThreadingModel= free
Not ThreadingModel= both

My COM object was deinfitely running in the MTA and the .NET Main thread entered an STA. Want me to post the code?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft. com/microsoft.publi c.dotnet.langua ges.csharp/<er************ **@TK2MSFTNGP14 .phx.gbl>
"Richard Blewett [DevelopMentor]" <ri******@NOSPA Mdevelop.com> wrote in
message news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
OK - I thought I better check this to ensure they didn't break it since
1.0 alpha (I tested it out then as I was teaching Essential COM in those
days).

I just wrote an MTA COM object which took a BSTR as a parameter (
non-blittable) and called it from an application with the STAThread
attribute on the Main. So both the Interop and the COM marshaller were
involved. Works fine.


Richard,

I suppose that with MTA COM object you mean a "free-threaded" apartmentmodel
COM object, else if it's marked "Both" it will be created on the main STA
thread. Sure, I ain't telling you something new, just to be sure ;-)

Willy.


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.782 / Virus Database: 528 - Release Date: 22/10/2004

[microsoft.publi c.dotnet.langua ges.csharp]
Nov 16 '05 #17
"Its not specifically that they are STA based."

The OP states that he or she is able to make it work if the .Net client
is a Windows Forms App, which obviously means that the issue was
resolved because of the [STAThread] attribute to the Main method. So, I
could safely conclude that the ActiveX control was built on STA model
and that the error was not due to any UI issue.

with regards,
J.V.Ravichandra n
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandra n+J.V.&cob=aspn etpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID= P3966388&BN=999 &PN=2
- Or, just search on "J.V.Ravichandr an"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #18
You can only safely conclude that the ActiveX control does not function correctly when loaded into the Host STA (a special STA that apartment threaded components get loaded into when created from the MTA). IIRC this was to do with the way the Windows UI interacts on the Host STA - but its been a few years since I did alot of deep COM work so I might be wrong.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

"Its not specifically that they are STA based."

The OP states that he or she is able to make it work if the .Net client
is a Windows Forms App, which obviously means that the issue was
resolved because of the [STAThread] attribute to the Main method. So, I
could safely conclude that the ActiveX control was built on STA model
and that the error was not due to any UI issue.

Nov 16 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2766
by: Nicolas Fleury | last post by:
Hi, I've made a small utility to re-raise an exception with the same stack as before with additional information in it. Since I want to keep the same exception type and that some types have very specific constructors (which take, for example, more than one parameter), the only safe way I have found to made it is by hacking the str representation: import sys
1
4231
by: Old Wolf | last post by:
1. What is the difference between #include <stdexcept> and #include <exception> ? 2. Is there a list somewhere of what each standard exception is used for? either to throw them, or throw user-defined exceptions derived from them? (for example I have been deriving mine from std::bad_alloc if there was a memory problem, or std::bad_exception if there was some other problem) 3. Is it a good idea to make all user-defined exceptions derive...
11
3273
by: Master of C++ | last post by:
Hi, I am writing a simulation package in C++, and so far I've written about 8000 lines of code and have about 30 classes. I haven't used C++ exceptions so far (for various reasons). The only two "resources" I use are memory and file I/O and whenever there is a memory allocation failure or file I/O failure I just simply call a custom assert-type function to check, print a error message and abort. This seems to be OK for now (for the...
4
15839
by: maricel | last post by:
I have the following base table structure - DDL: CREATE TABLE "ADMINISTRATOR"."T1" ( "C1" INTEGER NOT NULL ) IN "TEST_TS" ; ALTER TABLE "ADMINISTRATOR"."T1" ADD PRIMARY KEY
44
4225
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box, etc.). 2. Low-level operations that are used to carry out the high level tasks
40
13527
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
6
1763
by: Vadivel Kumar | last post by:
I've a problem in handling a custom exception The following is my custom exception class: public class AppException : public Exception { public AppException (string message, Exception innerException) { } public override string Message
3
2511
by: JohnDeHope3 | last post by:
First let me say that I understand that Asp.Net wraps my exception in an HttpUnhandledException. I have found a lot of discussion about that on the web, which was informative, but not helpful. Let me explain how my question is different. Second, let me say that I have two questions. One is an ASP.Net related question. The other is a general System.Exception question. They are related but also individual. Okay here is my problem...
7
5144
by: Sek | last post by:
Hi Folks! I was pondering over a code and noticed that exception handlers were present in the private, protected as well as public methods. And, ofcourse, public methods were calling priv/prot methods internally. My thought was, the exception is being rethrown and propagated by the non-public methods to the public methods, causing performance overhead (stack winding etc). I do agree that, the purpose of throwing the exception by...
2
6992
by: Darko Miletic | last post by:
Recently I wrote a dll in c++ and to simplify the distribution I decided to link with multithreaded static library (/MT or /MTd option). In debug everything works fine but in release I get this: parseMetadata.obj : error LNK2001: unresolved external symbol "public: __thiscall std::exception::exception(void)" (??0exception@std@@QAE@XZ) 2>libcpmt.lib(string.obj) : error LNK2001: unresolved external symbol "public: __thiscall...
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9873
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3806
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.