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

[STAThread] F1 F1

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Justine" <am***********@hotmail.com> wrote in message
news:8b****************************@phx.gbl...
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

Nov 15 '05 #2

P: n/a
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 thatmethod to indicate that when dealing with COM interop, the thread making thecall will be a single-threaded apartment.

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

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

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Justine" <am***********@hotmail.com> wrote in message
news:8b****************************@phx.gbl...
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

.

Nov 15 '05 #3

P: n/a
Justine wrote:

Just to add to what Nicholas has said:
called from that thread. By default, threads in .NET have no COM
apartment, it has to be explicitly set.

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 ev******@zicf.bet 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]
- mv*@spam.guard.caspershouse.com

"Justine" <am***********@hotmail.com> wrote in message
news:8b****************************@phx.gbl...
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

.

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.