Invoke a delegate on a specific thread C#

I think the best solution is to use Task objects and queue them to an StaThreadScheduler running a single thread.

Alternatively, you could use the ActionThread in Nito.Async to create a normal thread with a built-in queue of Action delegates.

However, neither of these will directly address another need: the ability to "pause" one action and continue with another one. To do this, you'd need to sprinkle "synchronization points" throughout every action and have a way to save its state, re-queue it, and continue with the next action.

All that complexity is very nearly approaching a thread scheduling system, so I recommend taking a step back and doing more of a re-design. You could allow each action to be queued to the ThreadPool (I recommend just having each one be a Task object). You'll still need to sprinkle "synchronization points", but instead of saving state and re-queueing them, you'll just need to pause (block) them.


Unfortunately there really is nothing built in to do this on any generic thread. You can accomplish this by creating a class that wraps a Thread and implements ISynchonizeInvoke.

A simple approach is to create an event processing queue on the dedicated thread as LBushkin mentions. I suggest using a Queue<Action> class and calling the Action delegate directly. You can accomplish most tasks that you would need using anonymous delegate actions.

Finally, just as a word of warning, I would suggest you use a Semaphore or EventWaitHandle instead of Thread.Sleep on your dedicated thread. It is definitely more friendly than executing your background loop over and over again when its unnecessary.