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
11
votes
6 answers

Convert Keith Hill's PowerShell Get-Clipboard and Set-Clipboard to a PSM1 script

I'd like to convert Keith Hill's C# implementation of Get-Clipboard and Set-Clipboard into pure PowerShell as a .PSM1 file. Is there a way to spin up an STA thread in PowerShell as he does in his Cmdlet when working with the clipboard? The Blog…
Eric Schoonover
  • 47,184
  • 49
  • 157
  • 202
11
votes
3 answers

The calling thread must be STA, because many UI components require this error In WPF. On form.show()

Firstly I have read several answers to similar questions on the site but to be honest I find them a bit confusing (due to my lack of experience rather than the answers!). I am using a the FileSystemWatcher() class to monitor a folder for a file…
Chief Wiggum
  • 121
  • 1
  • 2
  • 7
9
votes
3 answers

How to run NUnit test in STA thread?

We are in the process of porting a WPF app to .NET Core 3, preview 5. Some NUnit tests need to run in STA threads. How can this be done? None of the attributes like [STAThread], [RequiresSTA], ... work. This also doesn't work: [assembly:…
Sven
  • 175
  • 1
  • 8
9
votes
1 answer

Is Powershell -sta (apartment state) preferred?

I've been dabbling in Powershell (2.0) for the past several months and would love to use it to modernize and standardize some of the processes at work - mostly DOS based processes. Because of the nature of the work, there could be around 100…
Barb
  • 93
  • 1
  • 4
8
votes
1 answer

How to tell thread-pool to run a delegate on a `STA` thread?

I need a thread-pool for working with COM objects inside an ASP.NET project. QueueUserWorkItemSTA(WaitCallback)
Xaqron
  • 29,931
  • 42
  • 140
  • 205
8
votes
6 answers

Process.Start is blocking

I'm calling Process.Start, but it blocks the current thread. pInfo = new ProcessStartInfo("C:\\Windows\\notepad.exe"); // Start process mProcess = new Process(); mProcess.StartInfo = pInfo; if (mProcess.Start() == false) { …
Luca
  • 11,646
  • 11
  • 70
  • 125
8
votes
2 answers

SetApartmentState and [STAThread]

In Watin's source code, there is this piece of code: public void NavigateToNoWait(Uri url) { var thread = new Thread(GoToNoWaitInternal); thread.SetApartmentState(ApartmentState.STA); thread.Start(url); …
Odys
  • 8,951
  • 10
  • 69
  • 111
7
votes
2 answers

How to pump message for COM STA threads in C#?

I have a main STA thread that calls a lot methods on the COM object and a secondary STA thread that does a lot work on the same object too. I want the main thread and the secondary thread to work in parallel (i.e. I expect interlaced output from the…
Charlie
  • 764
  • 2
  • 13
  • 24
7
votes
4 answers

How many 'STA' threads can coexist inside a process?

I need multiple STA threads inside my ASP.NET application to use some COM components. I read somewhere inside each process, only one STA thread can exist. I coded a sample project and made many threads and set their apartment state to STA and they…
Xaqron
  • 29,931
  • 42
  • 140
  • 205
7
votes
1 answer

C# STAThread COMException

I have an external component (C++), which I want to call from my C# code. The code is something like this: using System.Text; using System.Threading; using System.Threading.Tasks; namespace dgTEST { class Program { [STAThread] …
Zoltán Barna
  • 463
  • 2
  • 4
  • 12
6
votes
2 answers

Is there a way to programmatically set the ApartmentState to STA?

I'm working on a GUI in PowerShell where I was throwing errors when certain comboboxes were clicked. After the error was thrown, I could drop the combobox list down and see it's contents, but if I shifted to another combox on the same datagridview,…
thepip3r
  • 2,855
  • 6
  • 32
  • 38
6
votes
2 answers

How are STA COM components handled when used in a WCF service hosted in IIS (7+)?

From what I understand, when a COM component marked as using STA is used from an MTA thread, the calls are supposed to be marshalled to an STA thread and executed from that dedicated thread. In the case of a Windows client application, this would…
SoftMemes
  • 5,602
  • 4
  • 32
  • 61
6
votes
1 answer

Use [STAThread] in a console program in C#

I have written code for a C# console application. It copies a clipboard value to a file, and it runs without any error. Now I want to use it in another C# project with other code. I use [STAThread] after class{}, but it give me an error: ::…
user2442074
6
votes
3 answers

"The calling thread must be STA" workaround

I know there are a few answers on this topic on SO, but I can not get any of the solutions working for me. I am trying to open a new window, from an ICommand fired from within a datatemplate. Both of the following give the aforementioned error when…
Jamona Mican
  • 1,574
  • 1
  • 23
  • 54
5
votes
2 answers

Threading issues in C# from external process

I have this simple code: public void Run() { var invokerThread = new Thread(new ThreadStart(RunOnBackground)); invokerThread.Start(); } private void RunOnBackground() { Trace.WriteLine("hi"); ... } Unfortunately when running this code…
Aaron
  • 2,236
  • 1
  • 16
  • 28
1
2
3
15 16