What does "[STAThread]" do?

閲覧: 29 回
最初の未読メッセージにスキップ

Konrad Den Ende

未読、
2002/05/10 12:50:052002/05/10
To:
I guess it's starting a thread but i'd like a more specific
explaination. MSDN was not of much help...

--

Vänligen
Konrad
-------------------------------------------------------
URL: http://konrads.webbsida.com
E-Mail: kon...@voxway.com
Phone: (+46) 0708 - 70 73 92
ICQ: 132 250 451

Bret

未読、
2002/05/10 12:57:552002/05/10
To:
I believe that STA stands for "single threaded apartment" Thread...

Bret


"Konrad Den Ende" <kon...@voxway.com> wrote in message
news:eAsg9KE#BHA.2252@tkmsftngp02...

Greg Ewing

未読、
2002/05/10 12:59:402002/05/10
To:
Konrad, this attribute indicates that this application will use the single
threaded apartment model by default.

Check out
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemSTAThreadAttributeClassTopic.asp

--
Greg
http://www.claritycon.com/

"Konrad Den Ende" <kon...@voxway.com> wrote in message
news:eAsg9KE#BHA.2252@tkmsftngp02...

Willy Denoyette [MVP]

未読、
2002/05/10 15:28:502002/05/10
To:
This attribute initializes a "COM Single Threaded Apartment" and makes the thread running Main to enter that STA.
This attribute is set by default for VB.NET application, C# applications have to explicitly set this.
Note that this is only required when you intend to create COM object instances, on the main thread, requiring to run on an STA, and
for all Winform applications.

Willy.

"Konrad Den Ende" <kon...@voxway.com> wrote in message news:eAsg9KE#BHA.2252@tkmsftngp02...

Arvind.R

未読、
2002/05/10 18:33:042002/05/10
To:

Hi,

From the docs,

"To decouple the client from an object’s concurrency and reentrancy
constraints, COM has a very formal abstraction that models how objects are
related to both processes and threads. This abstraction is formally called
an apartment.1 Apartments define a logical grouping of objects that share a
common set of concurrency and reentrancy constraints. Every COM object
belongs to exactly one apartment; however, one apartment can be shared by
multiple objects. The apartment an object belongs to is implicitly part of
an object’s identity. An apartment is neither a process nor a thread;"

If you would like to know more about this, please check this:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnesscom/ht
ml/c5interfaceimplementationrevisited.asp

Thanks.

___________

This posting is provided "AS IS" with no warranties, and confers no rights.
___________


--------------------
| From: "Konrad Den Ende" <kon...@voxway.com>
| Subject: What does "[STAThread]" do?
| Date: Fri, 10 May 2002 18:50:05 +0200
| Lines: 17
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
| Message-ID: <eAsg9KE#BHA.2252@tkmsftngp02>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: giraffen.ostkupan.studenthem.gu.se 193.11.232.67
| Path:
cpmsftngxa08!cpmsftngxa07!tkmsftngxs02!cpmsftngxa10!tkmsftngp01!tkmsftngp02
| Xref: cpmsftngxa08 microsoft.public.dotnet.languages.csharp:59794
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

Konrad Den Ende

未読、
2002/05/10 18:45:442002/05/10
To:
That was one great explaination with only one small
downside. I didn't get that. I don't doubt it's correct
info you served me but i'm more confused now than
before.

Would you care to enlight me with an example?

ptran

未読、
2002/05/11 19:26:262002/05/11
To:
Konrad,

STAs are only applicable to the COM world. The reason why .NET includes
it here is because there are still some COM/OLE calls implemented by the
framework. If you write .NET code then you have nothing to worry about.
But, if you perform com-interop on STA COM objects, then the [STAThread]
attribute is there to support this feature.


"Konrad Den Ende" <kon...@voxway.com> wrote in message

news:efpgsRH#BHA.1516@tkmsftngp02...

Konrad Den Ende

未読、
2002/05/12 5:55:102002/05/12
To:
I still don't quite understand it (just a little bit).

BUT - now i know what to do with it. Thank you.

Willy Denoyette [MVP]

未読、
2002/05/12 15:23:442002/05/12
To:
If you write Winforms applications you better worry about, they NEED and STA to run.

Willy.

"ptran" <ptra...@hotmail.com> wrote in message news:#GqD3NU#BHA.1692@tkmsftngp05...

-glenn-

未読、
2002/05/12 16:32:412002/05/12
To:
ptran, your paragraph isn't logically consistent to me. First you say that
STAThread is necessary because some of the Framework Class Library methods
are wrappers around standard COM/COM+ stuff. Then you say that if you write
.NET code STAThread isn't necessary. Whether you are doing interop
explicitly or implicitly (via FCL routines) then you will need STAThread.

-glenn-

"ptran" <ptra...@hotmail.com> wrote in message
news:#GqD3NU#BHA.1692@tkmsftngp05...

Konrad Den Ende

未読、
2002/05/12 17:53:132002/05/12
To:
The confusion comes from the fact that i left that part out
and still can run my programs. I'm not saying that you are
wrong. I'm just calling you a damn liar!

No, just kidding! Hope you're not too sensitive.

I actually believe you. I just wonder WHEN the problems
may hit me. What components (GUI, you ment, i guess) will
give me worries?

ptran

未読、
2002/05/13 1:10:132002/05/13
To:
glenn,

There is no such thing as apartments in .NET. All .NET components are
run as equivalent to COM's free threaded model. So, if you develop
*entirely* in .NET then you do not have to worry about any apartment model.
Hence, [STAThread] is irrelevent here. However, if you write .NET code that
requires com-interop to COM objects (and the COM object is marked as STA)
then [STAThread] is there to make both parties co-exist correctly. The
reason why [STAThread] is placed there is because the framework still calls
out to some COM objects marked as STA (ie. OLE drag&drop). Whether the
Base Class Library calls out to COM should not matter to the developer. The
[STAThread] attribute is placed there so you do not have to worry about this
issue. In summary, [STAThread] is required by the framework; and
conditional for the developer. Either way, [STAThread] is there for a good
reason.


"-glenn-" <som...@example.com> wrote in message
news:#hO1qQf#BHA.2344@tkmsftngp02...

[MVP] Thomas Tomiczek

未読、
2002/05/13 1:19:412002/05/13
To:
Ok,

problems hsit you one you are using anything on your WinForm that uses
(a) ActiveX Controls (STA per definition)
(b) certain windows controls that need STA.

Most of the time, changing the STAThread to MTAThread works fine. Just not
always. NOT using ANY attribute is bound to be erroneus.

Now, what can hit you (examples):

(a) Timer Control
(b) DirectShow
(c) Common Controls (SOME)

Also remember to ALWAYS use Invoke when calling Winform Control functions
from another thread than the one creating them.

And then read up all the big books about COM...

Believe me - Willy is not a lyer. This will come band haunt you, and then
you have a serious problem. SADLY problems turn up pretty late :-(

--
Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

"Konrad Den Ende" <kon...@voxway.com> wrote in message

news:eOeqr9f#BHA.2552@tkmsftngp05...

Konrad Den Ende

未読、
2002/05/13 4:38:582002/05/13
To:
Thank you. I think i get the general idea of the issue.

I still wonder, thou... Why do i have to type in that
thing? If it's so necsessary - why isn't it inserted
for me instead (kind of, like standard constructor)?

Is there some cases when it's better NOT TO use
that phrase (or is it a key word?)?

[MVP] Thomas Tomiczek

未読、
2002/05/13 5:26:362002/05/13
To:
IF you open a new winforms based project under VS.NET, [STAThread] is
automatically added to your main thread.

It is not something to set on the form, but on the thread before creating
the tmessage pump :-)

StaThread is an attribute. Actually it looks like this:

[STAThread]
public static main () {
}

--
Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

"Konrad Den Ende" <kon...@voxway.com> wrote in message

news:Odanfml#BHA.1648@tkmsftngp02...

Willy Denoyette [MVP]

未読、
2002/05/13 9:06:422002/05/13
To:
You don't have to type in that 'thing', you can always use VB.NET where the compiler automatically emits the STA attribute on an
application's entry point (Main), and VS.NET marks the Main method with STAThread by default for Windows and Console applications..

And no, there is no reason NOT to use this attribute in a Winforms application, COM objects marked 'Single' threaded must live in an
STA, those marked 'Both' can live in an STA and a MTA. Only those marked 'Free' must be created in an MTA.

In C# when you don't set the attribute, the main thread will enter an MTA initialized by the CLR, when you create an STA object
(remember a lot of .NET controls need an STA), the COM runtime will initialize an STA for the object to live in and return a proxy
to the calling MTA thread (that's one of the beauties of COM). Whenever you call a method on the object, the call will be marshaled
and handled by a thread from the COM run-time (STA) thread pool, while this works (another beauty of COM), performance suffers from
the extra marshaling involved.
But there is more, sometimes it's not possible to create a proxy/stub, because the object has no marshaler registered.
Some COM objects, like CDO can't simply be called from an MTA thread and will return an error when you try do so, others can't
return anything they simply deadlock.

So I guess by now you have enough reasons to be mindful when of the requirements of any COM you use in managed code.

And, you don't have to believe me, but please don't call me a liar ;-).

Willy.

"Konrad Den Ende" <kon...@voxway.com> wrote in message news:Odanfml#BHA.1648@tkmsftngp02...

-glenn-

未読、
2002/05/13 10:08:092002/05/13
To:
ptran, yes, I understand all that. You're missing what I'm pointing out in
how you describe this. It is not true that "if you develop entirely in .NET
then you do not have to worry about any apartment model". .NET uses interop
to implement a lot of the FCL functionality, therefore there really is
nothing like "developing entirely in .NET".

I think what you are trying to say is that if you develop entirely with
*managed* code (which is pretty tough, given how much of the FCL uses
interop) then you wouldn't need the STAThread attribute.

-glenn-

"ptran" <ptra...@hotmail.com> wrote in message

news:udktnyj#BHA.2384@tkmsftngp02...

[MVP] Thomas Tomiczek

未読、
2002/05/13 10:21:272002/05/13
To:
Glkenn,

getting picky -

> I think what you are trying to say is that if you develop entirely with
> *managed* code (which is pretty tough, given how much of the FCL uses
> interop) then you wouldn't need the STAThread attribute.

IO have some COM objects here written in managed code that require STL :-)
No, they do NOT use anything like WinForms etc. They just require STL :-)

--
Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

"-glenn-" <som...@example.com> wrote in message
news:uv50ceo#BHA.1692@tkmsftngp05...

-glenn-

未読、
2002/05/13 11:27:252002/05/13
To:

"[MVP] Thomas Tomiczek" <t.tom...@thona-consulting.com> wrote in message
news:3cdfcbee$1...@exchange.thona.root...
> Glkenn,
>
> getting picky -

Not really. ptran is just not explaining this well at all.

>
> > I think what you are trying to say is that if you develop entirely with
> > *managed* code (which is pretty tough, given how much of the FCL uses
> > interop) then you wouldn't need the STAThread attribute.
>
> IO have some COM objects here written in managed code that require STL :-)
> No, they do NOT use anything like WinForms etc. They just require STL :-)

STL? Structured Template Library? Something else?

[MVP] Thomas Tomiczek

未読、
2002/05/13 11:36:382002/05/13
To:
No, sorry, STA :-)

--
Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

"-glenn-" <som...@example.com> wrote in message

news:ulRgvKp#BHA.2044@tkmsftngp04...

-glenn-

未読、
2002/05/13 11:56:132002/05/13
To:
I'm sorry, but call me dense. ("You're dense", screams the audience of "Win
Ben Stein's Money". :-)

Isn't a COM object by definition not managed code? I'm probably missing
something fundamental here.

Thanks.

-glenn-

"[MVP] Thomas Tomiczek" <t.tom...@thona-consulting.com> wrote in message

news:3cdfde46$1...@exchange.thona.root...

[MVP] Thomas Tomiczek

未読、
2002/05/13 12:13:542002/05/13
To:
No,

a COM object is basicallly a C++ class :-) An interface a function pointer
table.

Managed code is "managed C++" and the "higher level languages". Things that
run in the virutal runtime - and COM was around way before.

--
Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

"-glenn-" <som...@example.com> wrote in message

news:eBS61ap#BHA.372@tkmsftngp04...

Willy Denoyette [MVP]

未読、
2002/05/13 13:03:512002/05/13
To:
Absolutely right, there is no such thing as "pure .NET", a lot of the FCL rely on COM interop like:

- System.Messaging
- System.DirectoryServices.
- System.Management
- System.EnterpriseServices.

Most of them take care they create object references without incurring marshaling overhead.
As an example the Management classes use the "Free" threaded Wbem provider to connect to WMI. When the caller is not running on an
MTA thread, the class creates a worker thread and enters the MTA before starting the thread that creates the object. So for those
classes it's not important you run Main on an STA thread or an MTA thread.

But Windows Forms is a different story, windows controls are Apartment threaded due to their ties to Win32, this is why
STAThreadAttribute is needed for Windows Forms applications in languages that don't set this attribute by default.
Windows controls may only be invoked on the thread the control was created on. Without the STA attribute there is no guarantee this
will be the case, most of the time the proxy/stub marshaler does a good job synchronizing accesses, but some controls will fail
miserably when referenced from an MTA, others will simply deadlock, resulting in non responding UI .

Willy.

"-glenn-" <som...@example.com> wrote in message news:uv50ceo#BHA.1692@tkmsftngp05...

ptran

未読、
2002/05/13 17:36:512002/05/13
To:
glenn,

I see what you're getting at now. I was trying to describe [STAThread]
as high level as possible so that Konrad could understand without being
further perplex with this issue. I'm pretty sure we both understand this
concept quite well. At any rate, your post has lead to a very good
discussion on this topic. Regards ;-).


"-glenn-" <som...@example.com> wrote in message

news:uv50ceo#BHA.1692@tkmsftngp05...

-glenn-

未読、
2002/05/13 20:22:282002/05/13
To:
Sounds good, ptran!

Hope Konrad is on-board with all this.

-glenn-

"ptran" <ptra...@hotmail.com> wrote in message

news:eH0S9Zs#BHA.1880@tkmsftngp02...

Konrad Den Ende

未読、
2002/05/14 3:00:572002/05/14
To:
Yes i am. I'm scared and cry in the nights, but then
who doesn't?

I got the gereral idea plus a number of usefull tips
that will save me from torning most of my hair of
my head during frustration.
--

Most Thankfully

全員に返信
投稿者に返信
転送
新着メール 0 件