473,624 Members | 2,565 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 #1
18 5703
Just a quick thought...

Are you missing "[STA Thread]" line in your Main() method?
[STAThread]
static void Main()
{
Application.Run (new Form1());
}

ShaneB

"Itzik" <it****@pisgasy s.co.il> wrote in message
news:uv******** ******@TK2MSFTN GP09.phx.gbl...
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 #2
i am using this
[STAThread]

static void Main()

{

Application.Run (new FrmDataEntryWei ghing());

}
"ShaneB" <st********@yah oo.com> wrote in message
news:uD******** ******@TK2MSFTN GP15.phx.gbl...
Just a quick thought...

Are you missing "[STA Thread]" line in your Main() method?
[STAThread]
static void Main()
{
Application.Run (new Form1());
}

ShaneB

"Itzik" <it****@pisgasy s.co.il> wrote in message
news:uv******** ******@TK2MSFTN GP09.phx.gbl...
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 #3
more information:
this form is not my start form.
"ShaneB" <st********@yah oo.com> wrote in message
news:uD******** ******@TK2MSFTN GP15.phx.gbl...
Just a quick thought...

Are you missing "[STA Thread]" line in your Main() method?
[STAThread]
static void Main()
{
Application.Run (new Form1());
}

ShaneB

"Itzik" <it****@pisgasy s.co.il> wrote in message
news:uv******** ******@TK2MSFTN GP09.phx.gbl...
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 #4
Are you creating any threads other than the main thread?

Without seeing some code or having a way to duplicate the error, it will be
hard to track down from here.

ShaneB

"Itzik" <it****@pisgasy s.co.il> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
more information:
this form is not my start form.
"ShaneB" <st********@yah oo.com> wrote in message
news:uD******** ******@TK2MSFTN GP15.phx.gbl...
Just a quick thought...

Are you missing "[STA Thread]" line in your Main() method?
[STAThread]
static void Main()
{
Application.Run (new Form1());
}

ShaneB

"Itzik" <it****@pisgasy s.co.il> wrote in message
news:uv******** ******@TK2MSFTN GP09.phx.gbl...
> 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 #5
I'll bet that the exception is being raised from a thread other than the main one that maybe calls Application.Run (new Form1()) on a form that has an ActiveX control on it (maybe without realising it). At the start of the thread function put a call to:

Thread.CurrentT hread.Apartment State = ApartmentState. STA;

By default, as a .NET created thread performs COM interop it initializes into the MTA

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/<#8************ **@TK2MSFTNGP15 .phx.gbl>

more information:
this form is not my start form.

Nov 16 '05 #6
Thank you is work
Can you explain me this

"Richard Blewett [DevelopMentor]" <ri******@NOSPA Mdevelop.com> wrote in
message news:uI******** ******@TK2MSFTN GP09.phx.gbl...
I'll bet that the exception is being raised from a thread other than the main one that maybe calls Application.Run (new Form1()) on a form that has an
ActiveX control on it (maybe without realising it). At the start of the
thread function put a call to:
Thread.CurrentT hread.Apartment State = ApartmentState. STA;

By default, as a .NET created thread performs COM interop it initializes into the MTA
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/<#8************ **@TK2MSFTNGP15 .phx.gbl>
more information:
this form is not my start form.

Nov 16 '05 #7
Your code is interfacing into the COM world and as such it has to conform to the way COM sees the world (COM knows nothing about .NET). COM is a binary standard and so needed a way to separate components that were written with thread sensitive toolkits (e.g. VB.NET) and thread sensitive technologies (windows UI, e.g. ActiveX controls) from thread agnostic code (code that really didn;t care what thread it was run on. To do this they created the apartment abstraction. There were mainly two types of apartment (well there were three but one isn't important for this discussion): Single Threaded Apartments (STAs) where a single thread processed all calls for objects that lived in it; the MTA where all other threads doing COM lived and objects there had no control over which thread called them.

All threads had to elect which of these types of apartment they wanted to be in when they initialized the COM runtime (it had to be initialized on every thread). Some types of components would just work slower if they were created from an apartment other than the one they wanted to be in, but some would plain just not work (e.g. ActiveX Controls).

.NET threads don't. by default, initialize the COM runtime as they generally don't need to call COM code. However, the one exception is thread that perform windows UI work. These will often ened up calling ActiveX controls as alot of bits of windows UI (especially the richer controls) are implemented this way. If a .NET thread starts doing COM work it will initialize the COM library based on its ApartmentState value. If the defayults are left as they are then the thread will enter the MTA.

Because many applications perform UI work on teh main thread the application (the one that calls Main), there is a special way to make it enter an STA (the STAThread attribute). However for other threads that attribute isn't acknowleged. So to make sure a thread enters an STA you have to set the ApartmentState of the thread manually.

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/<O#************ **@TK2MSFTNGP11 .phx.gbl>

Thank you is work
Can you explain me this

"Richard Blewett [DevelopMentor]" <ri******@NOSPA Mdevelop.com> wrote in
message news:uI******** ******@TK2MSFTN GP09.phx.gbl...
I'll bet that the exception is being raised from a thread other than the main one that maybe calls Application.Run (new Form1()) on a form that has an
ActiveX control on it (maybe without realising it). At the start of the
thread function put a call to:
Thread.CurrentT hread.Apartment State = ApartmentState. STA;

By default, as a .NET created thread performs COM interop it initializes into the MTA
Regards

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


Nov 16 '05 #8
In COM Interoperabilit y, when the COM and the .Net components both have
the same apartment models, the CLR's Interop Marshaler will be able to
manage the marshalling but when the apartment models differ if your COM
has a STA Apartment State and the .Net Componennt has a MTA model) then
the COM interop Marshaler will be invoked and since COM does not knopw
.Net's data types there will be an exception.

But it works if you use the Windows Application project because the
Windows Application project creates a Main method with the STAThread
attribute, which makes your .Net client a STA model just like your
ActiveX component hence there is an error. In the other case, your .Net
client was in MTA model and hence there was an exception.
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 #9
Are you really claiming you can't call across an apartment boundary from managed code to unmanaged 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/<u7************ **@TK2MSFTNGP09 .phx.gbl>

In COM Interoperabilit y, when the COM and the .Net components both have
the same apartment models, the CLR's Interop Marshaler will be able to
manage the marshalling but when the apartment models differ if your COM
has a STA Apartment State and the .Net Componennt has a MTA model) then
the COM interop Marshaler will be invoked and since COM does not knopw
.Net's data types there will be an exception.

But it works if you use the Windows Application project because the
Windows Application project creates a Main method with the STAThread
attribute, which makes your .Net client a STA model just like your
ActiveX component hence there is an error. In the other case, your .Net
client was in MTA model and hence there was an exception.
Nov 16 '05 #10

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

Similar topics

4
2755
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
4215
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
3262
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
15815
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
4206
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
13508
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
6
1758
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
2504
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
5124
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
6977
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
8685
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8633
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
8348
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
7176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
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
5570
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();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4187
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1493
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.