Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What are the relative merits (or demerits) for implementing an Erlang-style "Continuation" pattern in C#. I'm working on a project that has a large number of Lowest priority threads and I'm wondering if my approach may be all wrong.

It would seem there is a reasonable upper limit to the number of long-running threads that any one Process 'should' spawn. With that said, I'm not sure what would signal the tipping-point for too many thread or when alternate patterns such as "Continuation" would be more suitable.

In this case, many of the threads do a small amount of work and then sleep until woken to go again (Ex. Heartbeat, purge caches, etc...). This continues for the life of the Process.

share|improve this question
If they do a small amount of work and then sleep again, couldn't you just implement them all as a single thread with different possible tasks? This would solve your issue of having may threads – Dan McGrath Nov 30 '11 at 20:28
Isn't that essentially what the Continuation pattern is? But yes, that's what I'm considering. I'm just concerned about creating something that is too tightly coupled, relating functions that would otherwise never be combined together. That why I'm trying to think through the implications of following such a pattern. – JoeGeeky Nov 30 '11 at 20:33
TBH, no idea. I've yet to have time to look into Erlang, so couldn't reasonably comment on Erlang-style "Continuation" pattern. – Dan McGrath Nov 30 '11 at 20:37
up vote 4 down vote accepted

A thread model is really more for when a task is continually running. What you're describing is more of an event model. In response to a timer elapsing, you run through a function once, then the timer is reset. You only have enough threads to handle whatever events are running at any given moment, and you can have a ton of pending timers without even noticing.

Continuations are kind of a workaround to allow you to retain state in functional programming, but retaining state is what objects are all about. Store whatever you need in a field, and it will be available to you the next time the event fires.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.