Questions tagged [sta]

Single-Threaded Apartment of the Component Object Model (COM), as opposed to MTA

STA refers to the Single-Threaded Apartment of Microsoft's Component Object Model (COM). Its opposite is the Multiple Threaded Apartment (MTA), but there is also a Neutral Apartment.

Related tags:

References:

230 questions
105
votes
3 answers

STAThread and multithreading

From the MSDN article on STAThread: Indicates that the COM threading model for an application is single-threaded apartment (STA). (For reference, that's the entire article.) Single-threaded apartment... OK, that went over my head. Also, I read…
Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222
73
votes
4 answers

Single-threaded apartment - cannot instantiate ActiveX control

I need to get information about applied CSS styles in HTML page. I used AxWebBrowser and iterate IHTMLDOMNode. I'm able to get all the data I need and move the code into my application. The problem is that this part is running inside of the…
martin.malek
  • 2,166
  • 2
  • 19
  • 31
62
votes
4 answers

How to run unit tests in STAThread mode?

I would like to test an app that uses the Clipboard (WindowsForms) and I need the Clipboard in my unit tests also. In order to use it, it should run in STA mode, but since the NUnit TestFixture does not have a main method, I don't know where/how to…
Peter
  • 47,963
  • 46
  • 132
  • 181
43
votes
5 answers

How to run something in the STA thread?

In my WPF application I do some async communication (with server). In the callback function I end up creating InkPresenter objects from the result from server. This requires the running thread to be STA, which apparently it currently isn't.…
stiank81
  • 25,418
  • 43
  • 131
  • 202
28
votes
5 answers

How can I make a background worker thread set to Single Thread Apartment?

I am creating an automated test running application. In this part of the application, I am working on a polling server. It works by constantly polling the web server to determine when a new automated test should be run (for nightly automated runs…
KallDrexx
  • 27,229
  • 33
  • 143
  • 254
27
votes
2 answers

How to check apartment state of current thread?

I have a function which requires to be run in STA apartment state. I wan't to check if it is being run as STA, and if not spawn a new thread which runs in STA. How can I check which apartment state the current thread is being run in?
stiank81
  • 25,418
  • 43
  • 131
  • 202
27
votes
1 answer

Starting an STAThread in C#

I am still kind of new to C#, and especially threading in C#. I am trying to start a function that requires a single threaded apartment (STAThread) But I am not able to compile the following code: The function looks as follows in a separate class…
Saher Ahwal
  • 9,015
  • 32
  • 84
  • 152
20
votes
4 answers

Why are WinForms applications STAThread by default?

When you create an empty WinForms application with Visual Studio, the template has the STAThread attribute in the main application class. I have been reading some docs about it, but I'm not sure if I understood it at all. Really I have some…
Daniel Peñalba
  • 30,507
  • 32
  • 137
  • 219
17
votes
2 answers

What are the limitations of a STA thread in compare to MTA threads?

If we make a thread STA like this: Thread.SetApartmentState(STA); then it cannot run code marked with [MTAThread] attribute. We have seen [STAThread] in windows and console applications but I have never seen code with [MTAThread] attribute and don't…
Xaqron
  • 29,931
  • 42
  • 140
  • 205
16
votes
2 answers

How to post messages to an STA thread running a message pump?

So, following this, I decided to explicitly instantiate a COM object on a dedicated STA thread. Experiments showed that the COM object needed a message pump, which I created by calling Application.Run(): private MyComObj _myComObj; // Called from…
bavaza
  • 10,319
  • 10
  • 64
  • 103
15
votes
5 answers

.NET Windows Service needs to use STAThread

I have created a Windows Service that will be calling out to some COM components, so I tagged [STAThread] to the Main function. However, when the timer fires, it reports MTA and the COM calls fail. How can I fix this? using System; using…
Walter Williams
  • 944
  • 3
  • 11
  • 25
15
votes
2 answers

Why does WPF require a STAThread attribute to be applied to the Main method?

I am new to WPF, and in every tutorial I read, they either have a [System.STAThread] attribute applied to their Main method, or they tell the reader to do that. Is this attribute really "required"? And if so, why?
Madi D.
  • 1,980
  • 5
  • 23
  • 44
13
votes
3 answers

An MTA Console application calling an STA COM object from multiple threads

Although there are many questions about COM and STA/MTA (e.g. here), most of them talk about applications which have a UI. I, however, have the following setup: A console application, which is by default Multi-Threaded Apartment (Main() explicitly…
bavaza
  • 10,319
  • 10
  • 64
  • 103
12
votes
3 answers

COM outbound call results in "An outgoing call cannot be made since the application is dispatching an input-synchronous call."

I have a COM server (C++/STA (MFC based app)) and a COM client (C#/MTA). The COM server must live in an STA, since it's an MFC app (I have no choice in this matter). The client issues a call to the server, and the server issues a callback to the…
Jörgen Sigvardsson
  • 4,839
  • 3
  • 28
  • 51
11
votes
4 answers

Set ApartmentState for async void main

I have a Windows Forms app. Now I want to use an async method. Since C# 7.1 I can use an async Main method: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7-1 However, now my STAThread attribute is ignored and my app runs in MTA.…
Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
1
2 3
15 16