I am curious to know when STA/MTA are used in C# .net?
using (ManualResetEventSlim mre = new ManualResetEventSlim(false))
{
Thread _STAThread = new Thread(new ThreadStart(() =>
{
globalComObject = new ComClass();
mre.Set();
try
{
Thread.CurrentThread.Join();
}
catch (ThreadAbortException)
{ }
}));
_STAThread.SetApartmentState(ApartmentState.STA);
_STAThread.IsBackground = true;
_STAThread.Start();
mre.Wait();
}
You use them when doing interop with STA/MTA COM objects.
This stackoverflow answer would give you a plenty. Read also this and this MSDN page. The gist of it is that STA apartment is used for non thread-safe COM objects, while MTA can be used thread-safe COM objects in a multi-threaded fashion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With