Function of [stathread] in C #

Source: Internet
Author: User

C # keyword [stathread]: Single-threaded apartment
I:
Each thread has an attribute about apartmentstate, which can be set to STA, MTA, or unknown.
When you want to specify the project startup window, you need to declare a main () method in the window class and set the [stathread] attribute for this method.
For more information, seeArticle:

Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/cpguidnf/html/cpconmanagedunmanagedthreading. asp

Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/cpguidnf/html/cpconadvancedcominterop. asp

Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/cossdk/htm/pgservices_synchronization_8703.asp

2:
[stathread] indicates a single thread apartment single-threaded Suite, which is a thread model, used in the Program entry method
(in C # and VB. net is the main () method) to specify that the apartmentstate of the current thread is Sta. It does not affect other methods.
On the ASPX page, you can use aspcompat = "true" to achieve the same effect. This attribute is only useful in COM InterOP.
it is useless if all the attributes are managed code. [Stathread] indicates that the default thread model of an application is a single-threaded unit (STA ).
the startup thread model can be set to single-threaded or multi-threaded. If it is not set, the thread is not initialized. That is to say, if you use the. NET Framework,
and do not use com InterOP, this attribute is generally not required. Others include MTA (multi-threaded suite) and free thread ).
in a single-threaded suite, All accesses to objects in a single-threaded suite are transmitted through messages. Therefore, only one thread can access objects in a single-threaded suite at a time.

3:

in C #, what does [stathread] mean? How to use it?
single thread apartment

> Why is stathread required?

It changes the apartment state of the current thread to be single threaded

> Single thread apartment vs multithread apartment?

Correct: With the stathread attribute, you will be interacting with COM processes in
"Single threading apartment" model. Without it, you will be interacting with COM Processes
In the "Multiple threading apartment" model.

> So why do I need it... or why wocould I want it at some point?

You may want to interact with a com process in a mta model for performance reasons. You may
Want to interact with a com process in a sta model because of a design requirement. For example,
To use the Windows clipboard (system. Windows. Forms. Clipboard) You must be calling from a thread
Running in a sta. If the calling thread was started by your application you can set the apartmentstate
(System. Threading. apartmentstate) Before starting, but if you want to use the clipboard from your
Pplication's main thread, you need to use the system. stathread attribute on your main method.

> Why does main () only function as an entry point when it is declared static?

The simple answer is that is just the way that Microsoft designed the language. One way you can look
This though, is there shoshould only be 1 "instance" of your main method-the main method has nothing to do
With any specific instances of the class it is defined in, and shoshould therefore be static. In my opinion
It might have been a good idea to give the main method a property similar to a static contructor where it is
Executed once, and only once. Anyway, because the main method is static, You can execute your program
Having to create any arbitrary objects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.