[STAThread] F1 F1

J

Justine

hi all,

i just want to know the significance of [STAThread] in
the C# application. Why & for What reason is this used.


Thanz in Advance,
Justine
 
N

Nicholas Paldino [.NET/C# MVP]

Justine,

It is used to set the apartment state of the thread that executes that
method to indicate that when dealing with COM interop, the thread making the
call will be a single-threaded apartment.

The reason it is put there is in case any COM components are called from
that thread. By default, threads in .NET have no COM apartment, it has to
be explicitly set.

The attribute is most helpful on the entry point of an application. It
ensures that the threading model is set before any user code executes. Once
you set the apartment, you can not set it back.

Hope this helps.
 
J

Justine

Thanz a lot Nicholas...

i would appreciate if u could tell me how different is C#
from other .NET Programming languages. What is the role
of C# in the development of .NET Framework. i heard C# is
used in the development process of .NET Framework (i'm
not sure of it), is it true????

if u can guide me with some good links on the same would
of great help
-----Original Message-----
Justine,

It is used to set the apartment state of the thread that executes that
method to indicate that when dealing with COM interop, the thread making the
call will be a single-threaded apartment.

The reason it is put there is in case any COM components are called from
that thread. By default, threads in .NET have no COM apartment, it has to
be explicitly set.

The attribute is most helpful on the entry point of an application. It
ensures that the threading model is set before any user code executes. Once
you set the apartment, you can not set it back.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Justine said:
hi all,

i just want to know the significance of [STAThread] in
the C# application. Why & for What reason is this used.


Thanz in Advance,
Justine


.
 
R

Richard Grimes [MVP]

Justine wrote:

Just to add to what Nicholas has said:

By default .NET threads are not part of a COM apartment. If you use a COM
component or code that uses one, .NET *automatically* makes the thread join
a COM apartment. In COM there's essentially two types STA (which by
definition has just one thread, so a process can have multiple STA
apartments) and MTA which can have any number of threads (but there's only
one MTA per process). .NET has to know which apartment to make the thread
join and if you don't express a preference it will make the thread join the
process's MTA. If a component has a UI it *must* be in an STA, if the Main
thread is MTA then calls to the component will be marshalled (also,
re-entrancy is handled when a UI component runs in a STA). So wizard
generated code adds [STAThread] to Main to take this into account.
i would appreciate if u could tell me how different is C#
from other .NET Programming languages. What is the role
of C# in the development of .NET Framework. i heard C# is
used in the development process of .NET Framework (i'm
not sure of it), is it true????

That's a big question. All I can say is: take a look at the Shared Source
CLI (Rotor) that Microsoft provide free. You'll find that the basic runtime
is written in C++, but the libraries (the FCL) are written in C#. (Rotor is
not the released framework, but I guess there is a lot that is similar.)
Make up your own mind about the significance of the languages used to write
Rotor!

Richard
--
my email (e-mail address removed) is encrypted with ROT13 (www.rot13.org)
if u can guide me with some good links on the same would
of great help
-----Original Message-----
Justine,

It is used to set the apartment state of the thread that executes
that method to indicate that when dealing with COM interop, the
thread making the call will be a single-threaded apartment.

The reason it is put there is in case any COM components are
called from that thread. By default, threads in .NET have no COM
apartment, it has to be explicitly set.

The attribute is most helpful on the entry point of an
application. It ensures that the threading model is set before any
user code executes. Once you set the apartment, you can not set it
back.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Justine said:
hi all,

i just want to know the significance of [STAThread] in
the C# application. Why & for What reason is this used.


Thanz in Advance,
Justine


.
 

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