Skip to content

Instantly share code, notes, and snippets.

using System.Threading.Tasks;
namespace RStein.Async.Schedulers
{
public interface IProxyScheduler
{
bool DoTryExecuteTask(Task task);
TaskScheduler AsTplScheduler();
}
}
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;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace RStein.Async.Schedulers
{
public interface ITaskScheduler : IDisposable
{
int MaximumConcurrencyLevel
{
@renestein
renestein / gist:135209c69de14111fcb4
Created May 26, 2014 10:08
TaskSchedulerBase.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace RStein.Async.Schedulers
{
public abstract class TaskSchedulerBase : ITaskScheduler
{
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace RStein.Async.Schedulers
{
public class CurrentThreadScheduler : TaskSchedulerBase
{
private const int MAXIMUM_CONCURRENCY_LEVEL = 1;
public override int MaximumConcurrencyLevel
protected override ITaskScheduler Scheduler
{
get
{
return m_scheduler;
}
}
protected override IProxyScheduler ProxyScheduler
{
[TestMethod]
public async Task WithTaskFactory_When_One_Task_Is_Queued_Then_Task_is_Executed()
{
bool wasTaskExecuted = false;
await TestTaskFactory.StartNew(() => wasTaskExecuted = true);
Assert.IsTrue(wasTaskExecuted);
}
[TestMethod]
public async Task WithTaskFactory_When_Tasks_Are_Queued_Then_All_Tasks_Are_Executed()
{
const int NUMBER_OF_TASKS = 8096;
int numberOfTasksExecuted = 0;
var tasks = Enumerable.Range(0, NUMBER_OF_TASKS)
.Select(_ => TestTaskFactory.StartNew(() => Interlocked.Increment(ref numberOfTasksExecuted))).ToArray();
[TestMethod]
[ExpectedException(typeof (ObjectDisposedException))]
public void QueueTask_When_TaskScheduler_Disposed_Then_Throws_ObjectDisposedException()
{
var dummyTask = new Task(() => {});
Scheduler.Dispose();
Scheduler.QueueTask(dummyTask);
}
using System;
using System.Threading.Tasks;
namespace RStein.Async.Schedulers
{
public interface IAsioTaskService : IDisposable
{
Task Dispatch(Action action);
Task Dispatch(Func<Task> function);