473,473 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

What is [STAThread] what does it mean? what does it do?

I am at a loss with this. I tried to go back to basics, and start
learning all i didn't understand, starting at the top of the code file
generated by VS. But I can't seem to get any sort of start on this
problem, anything I read on the NET goes too far above my head to be of
any use.

For instance typical explanations say things like...

'The STAThreadAttribute marks a thread to use the Single-Threaded COM
Apartment if COM is needed'

What on this earth is a Single Threaded COM Apartment? I won't continue
quiting sentences of explanation that I don't understand.

But could someone please attempt to tell a beginner that does not know
anything about 'threading' what this instruction actually does?

Thankyou very much,

Gary.

Dec 2 '06 #1
4 19441
<ga********@myway.comwrote:
I am at a loss with this. I tried to go back to basics, and start
learning all i didn't understand, starting at the top of the code file
generated by VS. But I can't seem to get any sort of start on this
problem, anything I read on the NET goes too far above my head to be of
any use.

For instance typical explanations say things like...

'The STAThreadAttribute marks a thread to use the Single-Threaded COM
Apartment if COM is needed'

What on this earth is a Single Threaded COM Apartment? I won't continue
quiting sentences of explanation that I don't understand.

But could someone please attempt to tell a beginner that does not know
anything about 'threading' what this instruction actually does?
Probably the best way of reading up on this is to do a Google search
for "COM threading models" and read some explanations on the web.
Alternatively, buy one of the many, many COM books available.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 2 '06 #2
On 2 Dec 2006 14:31:55 -0800, ga********@myway.com wrote:
>I am at a loss with this. I tried to go back to basics, and start
learning all i didn't understand, starting at the top of the code file
generated by VS. But I can't seem to get any sort of start on this
problem, anything I read on the NET goes too far above my head to be of
any use.

For instance typical explanations say things like...

'The STAThreadAttribute marks a thread to use the Single-Threaded COM
Apartment if COM is needed'

What on this earth is a Single Threaded COM Apartment? I won't continue
quiting sentences of explanation that I don't understand.

But could someone please attempt to tell a beginner that does not know
anything about 'threading' what this instruction actually does?

Thankyou very much,

Gary.
A search for STAThread returns 211,000 results from Google.
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Dec 3 '06 #3
ga********@myway.com wrote:
I am at a loss with this. I tried to go back to basics, and start
learning all i didn't understand, starting at the top of the code file
generated by VS. But I can't seem to get any sort of start on this
problem, anything I read on the NET goes too far above my head to be of
any use.

For instance typical explanations say things like...

'The STAThreadAttribute marks a thread to use the Single-Threaded COM
Apartment if COM is needed'

What on this earth is a Single Threaded COM Apartment? I won't continue
quiting sentences of explanation that I don't understand.

But could someone please attempt to tell a beginner that does not know
anything about 'threading' what this instruction actually does?
No idea. I wish I knew what STAThread does. Apparently you need to declare
it for all WinForms programs.

My guess is that it is something to do with "message loops" getting confused
if they are used concurrently.

I think this underpins the main problem I've been having with F#. Unlike C#,
F# provides an interactive mode where you can type code and have it
executed immediately. Although it is supposed to make WinForms programming
easier (because you can play with your windows in real time) it has some
problems.

For example, when a file dialog box has been created the WinForms part of
your program can hang while the F# interactive mode waits for more input.
In order to use the dialog box you must enter something into the F#
interactive mode so the dialog box will get some CPU time.

There must be some magic incantation to spawn a new message loop for another
thread and then have the whole of my GUI work done in another thread, so it
won't block on the interactive mode's input. I've no idea what that is
though.

I'd love it if anyone here could shed some light on this or point me to
something more specific than google.com.

Best of luck!

--
Dr Jon D Harrop, Flying Frog Consultancy
Objective CAML for Scientists
http://www.ffconsultancy.com/product...ex.html?usenet
Dec 3 '06 #4
Single Thread Apartment vs MultiThread Apartment?

Correct: With the STAThread attribute, you will be interacting with COM
processes in a "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 would 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 application'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 at this though, is there should 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 should 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 without having to create any arbitrary objects.

For detail to see:
http://www.codeproject.com/com/CCOMThread.asp

Justin Li

<ga********@myway.com>
??????:11**********************@n67g2000cwd.google groups.com...
>I am at a loss with this. I tried to go back to basics, and start
learning all i didn't understand, starting at the top of the code file
generated by VS. But I can't seem to get any sort of start on this
problem, anything I read on the NET goes too far above my head to be of
any use.

For instance typical explanations say things like...

'The STAThreadAttribute marks a thread to use the Single-Threaded COM
Apartment if COM is needed'

What on this earth is a Single Threaded COM Apartment? I won't continue
quiting sentences of explanation that I don't understand.

But could someone please attempt to tell a beginner that does not know
anything about 'threading' what this instruction actually does?

Thankyou very much,

Gary.

Dec 4 '06 #5

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

Similar topics

2
by: Daniel | last post by:
what exact registry entries does the installing of a .net windows service create? either with installutil or with a deployment project.
1
by: tenpsa | last post by:
Hi, I have a C# application that uses the System.Diagnostics.Process functionality to execute other processes (to install a couple of .msi files). Here's my code so far: ...
9
by: plusk1008 | last post by:
I have finals next week and I am stuck on one question on my review sheet for excel. So once again I beg: Please, please, please, please, please, please, please, please, please, please someone help...
1
by: shaik mohamed abrar | last post by:
what do you mean by base 5 conversion?plz explain wit an example
1
by: hkhan20 | last post by:
Hi, I'm new to C programming and was just looking though some example code. i wanted to know what do ~= and ^= mean in C? please help. Thanks Huda
1
by: Natasha2401 | last post by:
If this was entered in the criteria field of an access database,what would it mean? !!
1
by: Monica Pnade | last post by:
what does /^/ mean in perl?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.