Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

threadpool

sta

I need a thread-pool for working with COM objects inside an ASP.NET project.

QueueUserWorkItemSTA(WaitCallback)
like image 926
Xaqron Avatar asked Dec 24 '10 20:12

Xaqron


People also ask

When should you not use thread pool?

Thread pools do not make sense when you need thread which perform entirely dissimilar and unrelated actions, which cannot be considered "jobs"; e.g., One thread for GUI event handling, another for backend processing. Thread pools also don't make sense when processing forms a pipeline.

When using a thread pool What happens to a given thread after it finishes?

Once a thread in the thread pool completes its task, it's returned to a queue of waiting threads. From this moment it can be reused. This reuse enables applications to avoid the cost of creating a new thread for each task. There is only one thread pool per process.

What is CLR thread pool?

The CLR thread pool contains two kinds of threads—the worker threads and the I/O completion port or IOCP threads. That means your ASP.Net worker process actually contains two thread pools: the worker thread pool and the IOCP thread pool. Naturally, these pools have different purposes.

Does task use thread pool C#?

Abstraction above the Threads. It uses the thread pool (unless you specify the task as a LongRunning operation, if so, a new thread is created under the hood for you).


1 Answers

From the CodeProject Article of Smart Thread Pool:

Also note that the .NET ThreadPool doesn't support calls to COM with single threaded apartment (STA), since the ThreadPool threads are MTA by design.

So I assume if you give the Smart Thread Pool a try, it could fit your requirements.

Personally, I use this class successfully since severals years.

like image 193
Uwe Keim Avatar answered Oct 19 '22 19:10

Uwe Keim