Created
February 21, 2014 09:39
-
-
Save JeffreyZhao/9131425 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LimitConcurrencyLevelScheduler : LocalScheduler | |
{ | |
private readonly ActionBlock<Action> _actionBlock; | |
public LimitConcurrencyLevelScheduler(int concurrencyLevel = 0, bool singleProducerConstrained = false) | |
{ | |
var options = new ExecutionDataflowBlockOptions | |
{ | |
MaxDegreeOfParallelism = concurrencyLevel <= 0 ? 1 : concurrencyLevel, | |
SingleProducerConstrained = singleProducerConstrained | |
}; | |
_actionBlock = new ActionBlock<Action>(a => a(), options); | |
} | |
public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action) | |
{ | |
if (dueTime.Ticks > 0) | |
throw new NotSupportedException(); | |
var m = new SingleAssignmentDisposable(); | |
_actionBlock.Post(() => | |
{ | |
if (!m.IsDisposed) | |
m.Disposable = action(this, state); | |
}); | |
return m; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment