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

asked15 years ago
last updated12 years ago
viewed36k times
Up Vote28Down Vote

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 of our GUI application).

When the polling server sees a request, it downloads all the information necessary and then executes the test run in a background worker. The problem is that part of the test run has OLE, COM, and other calls (for example, Clipboard.Clear()) that occur in the background worker thread. When one of these calls occurs, the following exception occurs:

Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.

How can I mark a background worker thread as single thread apartment? The Main call in my Program.cs obviously already has that attribute.