Note that there are some explanatory texts on larger screens.

plurals
  1. POC# equivalent for Java ExecutorService.newSingleThreadExecutor(), or: how to serialize mulithreaded access to a resource
    text
    copied!<p>I have a couple of situations in my code where various threads can create work items that, for various reasons, shouldn't be done in parallel. I'd like to make sure the work gets done in a FIFO manner, regardless of what thread it comes in from. In Java, I'd put the work items on a single-threaded <code>ExecutorService</code>; is there an equivalent in C#? I've cobbled something together with a <code>Queue</code> and a bunch of <code>lock(){}</code> blocks, but it'd be nice to be able to use something off-the-shelf and tested.</p> <p><strong>Update:</strong> Does anybody have experience with System.Threading.Tasks? Does it have a solution for this sort of thing? I'm writing a Monotouch app so who knows if I could even find a backported version of it that I could get to work, but it'd at least be something to think about for the future.</p> <p><strong>Update #2</strong> For C# developers unfamiliar with the Java libraries I'm talking about, basically I want something that lets various threads hand off work items such that all those work items will be run on a single thread (which isn't any of the calling threads).</p> <hr> <p><strong>Update, 6/2018</strong>: If I was architecting a similar system now, I'd probably use <a href="https://docs.microsoft.com/en-us/previous-versions/dotnet/reactive-extensions/hh242985(v=vs.103)" rel="nofollow noreferrer">Reactive Extensions</a> as per <a href="https://stackoverflow.com/a/50890708/27358">Matt Craig's answer</a>. I'm leaving <a href="https://stackoverflow.com/a/15799283/27358">Zachary Yates' answer</a> the accepted one, though, because if you're thinking in Rx you probably wouldn't even ask this question, and I think <code>ConcurrentQueue</code> is easier to bodge into a pre-Rx program.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload