473,889 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
+ 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 STAThreadAttrib ute 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 19485
<ga********@myw ay.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 STAThreadAttrib ute 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.co m>
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********@mywa y.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 STAThreadAttrib ute 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********@mywa y.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 STAThreadAttrib ute 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.Clipboar d) 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.Threadi ng.ApartmentSta te) before starting, but if
you want to use the clipboard from your application's main thread, you need
to use the System.STAThrea d 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********@myw ay.com>
??????:11****** *************** *@n67g2000cwd.g ooglegroups.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 STAThreadAttrib ute 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
1463
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
5526
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: System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo.Arguments = sArgs; proc.StartInfo.FileName = "msiexec.exe"; proc.StartInfo.WorkingDirectory = sPath; proc.Start();
9
76517
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 me!! What does ### mean in Excel and how do you fix it??? please,please,please help!! 8( I'm totally stressed over this one question!! You can answer here or by email.
1
3236
by: shaik mohamed abrar | last post by:
what do you mean by base 5 conversion?plz explain wit an example
1
1515
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
1463
by: Natasha2401 | last post by:
If this was entered in the criteria field of an access database,what would it mean? !!
1
3289
by: Monica Pnade | last post by:
what does /^/ mean in perl?
0
9962
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11198
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10889
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10442
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7993
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7150
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5829
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6029
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3256
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.