STA's and MTA's

R

Razzie

Hey all,

I somewhat know of the differences between STA's and MTA's - theoratically.
However, when it comes to practical use of this, I don't.

When I create a simple WinForms application, the main process runs in a STA
by default. I can change this to MTA if I want to. But, what is the use of
this? Is it bad? Good? Can anyone give me a practicle example of when you
should use STA and when MTA?

Cheers,

Razzie
 
R

Richard Blewett [DevelopMentor]

It is generally a bad idea as there are a nunber of UI things that use COM as a backbone. This will expect (due to the thread sensitivity of UI code) to have the code accessed from an STA thread. If you have any activeX controls on your form (you may have them unknowingly by using a 3rd party control) or you want to use OLE drap and drop (which won't be initialized if you use MTAThread) there will be issues.

Regards

Richard Blewett - DeveloopMentor
http://staff.develop.com/richardb/weblog

Hey all,

I somewhat know of the differences between STA's and MTA's - theoratically.
However, when it comes to practical use of this, I don't.

When I create a simple WinForms application, the main process runs in a STA
by default. I can change this to MTA if I want to. But, what is the use of
this? Is it bad? Good? Can anyone give me a practicle example of when you
should use STA and when MTA?

Cheers,

Razzie
 
N

Nicholas Paldino [.NET/C# MVP]

Razzie,

Aside from what previous posters have mentioned, is there a reason that
you want to do this? What benefit are you seeking from doing this?
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Razzie,

If you change the UI thread to be MTA you'll change it back very soon :)
You'll get an exception saying that STA is required by WindowsForms. That is
because WindowsForms uses COM. Chances not to get that exception are not
big.
 
R

Razzie

I scanned that blog - it's why I asked this question actually :) It's good
theory, but it does not explain the practical use of it AFAICT.

Thanks.
 
R

Razzie

Hello Nicholas,

It is pure curiousity and from an eductational point of view, actually. I've
made multi-threaded applications, but never knew about the STA and MTA. So I
was wondering - when do you use STA, when MTA, what are the differences in
terms of performance.

Thanks,

Razzie

Nicholas Paldino said:
Razzie,

Aside from what previous posters have mentioned, is there a reason that
you want to do this? What benefit are you seeking from doing this?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Razzie said:
Hey all,

I somewhat know of the differences between STA's and MTA's -
theoratically. However, when it comes to practical use of this, I don't.

When I create a simple WinForms application, the main process runs in a
STA by default. I can change this to MTA if I want to. But, what is the
use of this? Is it bad? Good? Can anyone give me a practicle example of
when you should use STA and when MTA?

Cheers,

Razzie
 
S

Scott Allen

Hi Razzie:

You can make your application multi-threaded without changing the
apartment state.

In general, the only time you want to explicitly set the apartment
state is if you are doing COM interop with STA components. You want
your thread apartment type to match the threading model of any COM
objects the thread uses to minimize performance hits. As other posters
pointed out, there are additional reasons for setting the STA state
for Main.

--
Scott
http://www.OdeToCode.com/blogs/scott/

Hello Nicholas,

It is pure curiousity and from an eductational point of view, actually. I've
made multi-threaded applications, but never knew about the STA and MTA. So I
was wondering - when do you use STA, when MTA, what are the differences in
terms of performance.

Thanks,

Razzie

Nicholas Paldino said:
Razzie,

Aside from what previous posters have mentioned, is there a reason that
you want to do this? What benefit are you seeking from doing this?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Razzie said:
Hey all,

I somewhat know of the differences between STA's and MTA's -
theoratically. However, when it comes to practical use of this, I don't.

When I create a simple WinForms application, the main process runs in a
STA by default. I can change this to MTA if I want to. But, what is the
use of this? Is it bad? Good? Can anyone give me a practicle example of
when you should use STA and when MTA?

Cheers,

Razzie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top