Created
May 26, 2014 09:27
-
-
Save renestein/05a5b45fb11f969f5d85 to your computer and use it in GitHub Desktop.
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
namespace RStein.Async.Schedulers | |
{ | |
public class ProxyScheduler : TaskScheduler, IProxyScheduler, IDisposable | |
{ | |
private readonly ITaskScheduler m_realScheduler; | |
public ProxyScheduler(ITaskScheduler realScheduler) | |
{ | |
if (realScheduler == null) | |
{ | |
throw new ArgumentNullException("realScheduler"); | |
} | |
m_realScheduler = realScheduler; | |
m_realScheduler.ProxyScheduler = this; | |
} | |
public override int MaximumConcurrencyLevel | |
{ | |
get | |
{ | |
return m_realScheduler.MaximumConcurrencyLevel; | |
} | |
} | |
public void Dispose() | |
{ | |
Dispose(true); | |
} | |
public virtual bool DoTryExecuteTask(Task task) | |
{ | |
if (task == null) | |
{ | |
throw new ArgumentNullException("task"); | |
} | |
bool taskExecuted = TryExecuteTask(task); | |
if (taskExecuted) | |
{ | |
task.RemoveProxyScheduler(); | |
} | |
return taskExecuted; | |
} | |
public virtual TaskScheduler AsTplScheduler() | |
{ | |
return this; | |
} | |
protected override void QueueTask(Task task) | |
{ | |
task.SetProxyScheduler(this); | |
m_realScheduler.QueueTask(task); | |
} | |
protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued) | |
{ | |
if (!taskWasPreviouslyQueued) | |
{ | |
task.SetProxyScheduler(this); | |
} | |
return m_realScheduler.TryExecuteTaskInline(task, taskWasPreviouslyQueued); | |
} | |
protected override IEnumerable<Task> GetScheduledTasks() | |
{ | |
return m_realScheduler.GetScheduledTasks(); | |
} | |
protected void Dispose(bool disposing) | |
{ | |
if (disposing) | |
{ | |
m_realScheduler.Dispose(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment